-->
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.  [ 2 posts ] 
Author Message
 Post subject: Get nextvalue from enhanced.TableGenerator without save
PostPosted: Fri Dec 07, 2007 9:20 am 
Newbie

Joined: Fri Dec 07, 2007 5:52 am
Posts: 4
Location: Budapest, Hungary, EU
Hi!

Is there a way to get the next value from an org.hibernate.id.enhanced.TableGenerator for a specific sequence without saving an object to database?

I would like to use org.hibernate.id.enhanced.TableGenerator for several tables to generate keys, however I have a special case where I need unique id-s without immadiate save to the database. So in theory I will have a configured TableGenerator and the question is can I use that TableGenerator to generate values for me, like a sequence can?

I tried to look for some HQL support for this but did not find anything.

Thank you for the answers.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 09, 2007 12:35 pm 
Newbie

Joined: Fri Dec 07, 2007 5:52 am
Posts: 4
Location: Budapest, Hungary, EU
After all I came up with the following (hibernate version 3.2.5GA).

Table01 is a class mapped to a database table. Its id generator is an enhanced.TableGenerator:
Code:
        <id name="id" type="long">
            <column name="id" />
            <generator class="org.hibernate.id.enhanced.TableGenerator">
               <param name="segment_value">seq_table01_id</param>
               <param name="initial_value">1</param>
               <param name="increment_size">10</param>
               <param name="optimizer">pooled</param>
            </generator>
        </id>


And the following code lets me generate id-s without an immadiate save. It has 3 unchecked casts but it works.
Code:
   Session hibSession = sessionFactory.openSession();
   long key = ((Long) ((AbstractEntityPersister) sessionFactory
         .getClassMetadata(Table01.class)).getIdentifierGenerator()
         .generate((SessionImplementor) hibSession, null))
         .longValue();
   hibSession.close();


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