Well, you can just indicate that you generate the id value by yourself, set the id in your entities, and then the data will be saved with the id you provided.
I do worry a little bit about this. You don't want to reinvent the wheel. Most databases provide id generation strategies that have been tried and tested by people much smarter and better looking than you and me put together. :)
You might want to look at the various javax.persistence.GenerationType objects:
These provide a fairly large amount of flexibility in how ids are generated, and they've been tested pretty good!
Please check out my tutorials on the topic of unique primary key id generation through Hibernate and the Java Persistence API. You might find it helpful!
http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=12oneclasstoonetablejpamapping
Quote:
here are actually four different strategies for having the primary key generated by the database. Those four options are:
* AUTO
* IDENTITY
* TABLE
* SEQUENCE
javax.persistence.GenerationType.AUTO
The AUTO generation strategy is the default, and this setting simply chooses the primary key generation strategy that is the default for the database in question, which quite typically is IDENTITY, although it might be TABLE or SEQUENCE depending upon how the database is configured. The AUTO strategy is typically recommended, as it makes your code and your applications most portable.
javax.persistence.GenerationType.IDENTITY
The IDENTITY option simply allows the database to generate a unique primary key for your application. No sequence or table is used to maintain the primary key information, but instead, the database will just pick an appropriate, unique number for Hibernate to assign to the primary key of the entity. With MySQL, the first lowest numbered primary key available in the table in question is chosen, although this behavior may differ from database to database.
javax.persistence.GenerationType.Sequence
Some database vendors support the use of a database sequence object for maintaining primary keys. To use a sequence, you set the GenerationType strategy to SEQUENCE, specify the name of the generator annotation, and then provide the @SequenceGenerator annotation that has attributes for defining both the name of the sequence annotation, and the name of the actual sequence object in the database.
Here's what the getId() method of the Snafu class would look like if we used a SEQUENCE GenerationType:
@Id
@SequenceGenerator(name="s1", sequenceName="SEQ")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="s1")
public long getId() {return id;}
javax.persistence.GenerationType.TABLE
The TABLE GenerationType allocates a separate database table to keep track of the generation of unique ids. To facilitate the description of the table to be used, the TABLE strategy works hand in hand with the @TableGenerator annotation. So, if our Snafu class was to use a separate table for managing primary keys, the getId() method annotation would look like this:
@Id
@TableGenerator(name="tg", table="pk_table",
pkColumnName="name", valueColumnName="value",
allocationSize=10
)@GeneratedValue(strategy=GenerationType.TABLE, generator="tg")
public long getId() {return id;}
With this @TableGenerator annotation, a separate table in the database named pk_table would be created with two columns, one called name, and the other called value. The name will simply be Snafu, the name of the class using the table, indicating the class for which the key is being maintained. The pkColumnValue will maintain the current iteration of key generation. Furthermore, the allocationSize determines the increment size of the generated primary keys for a new thread.
From here:
http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=12oneclasstoonetablejpamapping