Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubts in Hibernate key generators.

 
Siddharth Bhargava
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I read Hibernate key generators from the book Java Persistence with Hibernate. I have certain doubts.

1) native - It says that identity generators picks other identity generators such as identity, sequence or hilo. depending on the capabilities of the underlying database. It also says to use this when we want to keep our mapping metadata portable.
Q. - Can we know which generator does native picks ? (sequence, identity or hilo) . Also how does it make it portable.

2) identity - It says that it supports identity columns in DB2,MySQL,etc...
Q. What are identity columns? What do we mean by it here.?

3) hilo AND seqhilo-
Q. didn't understand how we can use it. ? Could you give me a practical example of this. ?

4) uuid.hex AND guid - I tried this with MySQL its not working. It gives an error. Also please explain me the concept behind these two.

5) sequence - It says that this generator creates a sequence.
Q. What do we mean by it. Please explain me with a practical example. ?

Please do clear my concepts and Help me. Thanks in advance.
 
Sathya Srinivasan
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Questions 2 and 5 can be understood if you read any good DB book (for example, MySQL's documentation). Since these are basic DB concepts, Hibernate expects you to already have an understanding of those concepts (like a pre-requisite).
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:




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)

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.


-Cameron McKenzie
[ September 10, 2008: Message edited by: Cameron Wallace McKenzie ]
 
Siddharth Bhargava
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am not doing the key generation with JPA annotations. I am understanding with simple mapping xml. Please do give an example of hilo, sequence hilo. I am not able to understand how to apply them practically. Please also help me understand uuid.hex and guid generators with a simple mapping example. Please do help me in clearing my concepts. !!!
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried, and what is not working?

Let us help you, but we can provide more help if you try some things and tell us what is or what is not working for you.

-Cameron McKenzie
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic