-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Doubts in Hibernate key generators.
PostPosted: Tue Jul 08, 2008 1:43 pm 
Newbie

Joined: Thu Feb 14, 2008 1:47 am
Posts: 9
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.2.5

Name and version of the database you are using: MySQL 5.0.45

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 Help me. Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 08, 2008 5:39 pm 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
If you have no special needs and you are using MySQL: just trust the database for this task and mark your entities (maybe the common base class!) like this:
Code:
@Id
   @Column(name = "ID")
   @GeneratedValue(strategy = GenerationType.AUTO)
   private Long id;


Hibernate will then table create statements like this:

Code:
create table dpjw.ADDRESS (
        ID bigint not null auto_increment,
(...)


Then, just be happy -:)

_________________
Carlo
-----------------------------------------------------------
please don't forget to rate if this post helped you


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 08, 2008 11:03 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
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:

Image

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

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 09, 2008 2:07 pm 
Newbie

Joined: Thu Feb 14, 2008 1:47 am
Posts: 9
Please help me in understanding hilo and sequence hilo with a simple example. Please also help me in understanding uuid.hex and guid. I want to understand the concepts of hibernate key generators throughly. Please do clear my doubts in other generators also if possible, with a simple example.
Thanks in advance.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.