Second option is to use an orm for java :Hibernate with Spring or not .
There are some options you can use:
Use this node in your xml configurations file for your mapped table from database.
<generator class=”name”/>
Where class may be increment with the usual meaning, identity (the identity column of one table), hilo (a generator of random HiLow long with a complicated algorithm).
Soon I will post a working example here.
More informations you could find on RoseIndiaTUTORIALS