-->
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: Using a Hibernate id generator outside of Hibernate?
PostPosted: Tue Mar 24, 2009 5:26 am 
Newbie

Joined: Wed Feb 25, 2009 8:48 am
Posts: 5
Dear all,

When I persist an entity I need to know a unique id for it before commiting the transaction. As stated in another thread, the id of an entity can only be obtained via getId() after commiting – at least if you want your code to be portable.

In order to overcome this, I want to add another field to my entity, getReference(), say, which stores an id which I manually set before calling session.save(). For this, I need a way to generate unique id's and I thought, I could use one of Hibernate's generators. How could I do this?

I need something like

UniqueIdSource.getNext()

which returns a new id each time it is called; for instance, it increments the current value and returns it. It should be synchronized and persist the id.

Can somebody give me a pointer on which Hibernate generator I could use for this and how?

It is important that UniqueIdSource.getNext() runs in its "own" transaction – not that two concurrently running sessions obtain the same id because neither of them has committed yet.

Thanks,
hbf

P.S. I am using Hibernate with Spring and a single data source.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 24, 2009 10:03 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
As stated in another thread, the id of an entity can only be obtained via getId() after commiting


Hmmm... I don't think this is entirely true. If you use session.save() to save your object it will return the id that has been assigned to that object. The thread you are referring to are discussing session.persist() which works differently. It doesn't return the id, and thus id generation can be delayed until later. In the "worst" case, until commit().


Top
 Profile  
 
 Post subject: Re:
PostPosted: Wed Jun 10, 2009 9:10 am 
Newbie

Joined: Wed Feb 25, 2009 8:48 am
Posts: 5
nordborg wrote:
Quote:
As stated in another thread, the id of an entity can only be obtained via getId() after commiting


Hmmm... I don't think this is entirely true. If you use session.save() to save your object it will return the id that has been assigned to that object. The thread you are referring to are discussing session.persist() which works differently. It doesn't return the id, and thus id generation can be delayed until later. In the "worst" case, until commit().


You are right! – My mistake. save() returns the id!

Apologies,
Hbf


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.