Sounds like you might be getting a little too hung up on primary key generation. Try to keep it simple, and use what best fits your needs.
Here's a class diagram for the javax.persistence.GenerationType:
Identity is real easy to use. In fact, for most databases, AUTO just defaults to identity. Making a primary key the identity column is as easy as creating a table with the following SQL:
Code:
CREATE TABLE table_name(id INTEGER AUTO_INCREMENT PRIMARY KEY)
As far as what a given database will default to, you really need to check the documentation. Or, you can just do a little test run and inspect the database tables that are created or used - that would answer your question without any debate.
Both Sequence and TABLE primary key generation strategies are fairly easy to implement themselves. Really, it's worthwhile just actually coding up a simple Java example and running that Hibernate3 sample code against your database to see what's happening. I've got a few examples using JPA annotations on my website if you're interested in checking them out. It'll really open your eyes to how Hibernate and database perform primary key generation.
Tutorial on Hibernate3 and Primary Key Generation Strategies with JPA Annotations (Mid way down)
Quote:
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.
Quote:
-Cameron McKenzie