-->
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: Basic question about Hibernate id generation
PostPosted: Tue Sep 06, 2005 3:30 pm 
Newbie

Joined: Tue Aug 30, 2005 12:02 pm
Posts: 7
I'm planning to convert a JDBC application to use Hibernate but I'm really confused about id generation. In the existing application a query of the form: "SELECT id_sequence.NEXTVAL FROM dual" has to be run to get the next available id.

I do not understand how a Hibernate id generator is going to know how to duplicate this query unless somewhere in the configuration it is given the sequence name. I don't see anything in the Hibernate mapping configuration where this information is entered. How can Hibernate figure out what to do to get the next sequence if it is not told the sequence name?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 06, 2005 4:14 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
I don't know if hibernate can get the next sequence number before it is generated in the database, but it sure can use them as they are intended to be used.

Something along these lines works. This will have hibernate insert rows into a table using the database's sequence algorithm to generate its primary key. The internal database sequence generator is named nameOf_seq_generator

Code:
        <id name="intSeqNo" type="java.lang.Integer" column="int_seq_no"
            unsaved-value="null">
            <generator class="native">
                <param name="sequence">nameOf_seq_generator</param>
            </generator>
        </id>



Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 06, 2005 4:36 pm 
Regular
Regular

Joined: Mon Aug 29, 2005 9:46 am
Posts: 102
Actually, Hibernate doesn't have to figure out what to do to get the next sequence number, because all it does is use the sequence from the database. Of course, this will only work if your database has this kind of mecanism. So hibernate looks for the database sequence related to the id you specified in the .hbm.xml and tells the database to generate the next number.


Top
 Profile  
 
 Post subject: Thanks!
PostPosted: Fri Sep 09, 2005 10:26 am 
Newbie

Joined: Tue Aug 30, 2005 12:02 pm
Posts: 7
Thanks to both who replied. My basic problem was not seeing that the sequence generator XML had a param for the DB sequence. I thought this had to be specified somewhere but I searched quite a bit and did not find the answer.

Thanks again, problem solved.


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.