-->
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.  [ 3 posts ] 
Author Message
 Post subject: How to write custom ID generator?
PostPosted: Sat Jul 19, 2008 10:52 am 
Newbie

Joined: Thu Jan 05, 2006 1:33 am
Posts: 6
HI,

I want to write custom id generator for my class. How can I do that? Is there any reference materials to read.

_________________
Krishna Srinivasan
Hibernate Tips
Hibernate Articles


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 19, 2008 1:31 pm 
Expert
Expert

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

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

_________________
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: Sat Jul 19, 2008 9:19 pm 
Newbie

Joined: Thu Jan 05, 2006 1:33 am
Posts: 6
Thanks a lot for your reply!!

_________________
Krishna Srinivasan
Hibernate Tips
Hibernate Articles


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.