-->
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.  [ 7 posts ] 
Author Message
 Post subject: Help needed for Native sequence and lazy as false
PostPosted: Thu Feb 28, 2008 12:50 pm 
Newbie

Joined: Thu Feb 28, 2008 12:35 pm
Posts: 15
Hi all,

i am new to Hibernate and i need your help in peculiar situation i am facing.

I am having a mapping file which has lazy="false" at class declaration level and employs native sequence generator from database i.e, sequence is present in the database which provides next id.

Our application whenever is creating an instance of this particular class say X, also increments the version_id before saving it to database.

Here is the sequence:
X x = new X();
saveEntity(x);

saveEntity(x) -- is just normal saveOrUpdate() wrapper.

The table contains entries not in ascending order.

rownum ---- x_id
-----------------
1 -- 101
2 --- 102
3-- 105
4 -- 103
5--104
------------------------

I am wondering what is causing this behavior. Is it possible that when i do X x = new X(), the hibernate fetches the x_id from the native sequence generator and attaches to it before the actual commit to the database. Meanwhile if the another user is creating another instance he will get next instance of x and also the next x_id.

SO, whoever commits to da database first wins, regardless who got the least x_id.

Although i think i am wrong, i need your help/comments.

thanks in advance,


Top
 Profile  
 
 Post subject: Re: Help needed for Native sequence and lazy as false
PostPosted: Thu Feb 28, 2008 1:01 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
You are right. The first commit makes it to database. If you want to double check this roll back the transaction and see if you lose ids.



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 28, 2008 1:12 pm 
Newbie

Joined: Thu Feb 28, 2008 12:35 pm
Posts: 15
Can you please elaborate on this behavior.. i mean why is it happening..is it because we had set lazy="false" at class level or is it that we are using native sequence generator.

why should hibernate pick the next sequence for the class id before the user saves the object to the database.

Is it not wrong say, like the user creates new x() but later decides not to commit that object to the database. In this scenario sequence id 103 is picked up but not used, while other users continue to next sequences and commit to the database.

please correct me, if i am wrong..


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 28, 2008 1:24 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
The id is not picked when an instance is creates since hibernate has no clue you are creating an entity. It is allocated when the call to save is made but calling save does not necessarily flush to database right away.


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 28, 2008 1:37 pm 
Newbie

Joined: Thu Feb 28, 2008 12:35 pm
Posts: 15
thanks once again for the clarification.

Did you mean to say hibernate got the next sequence when i saved the object but the object is not flushed(committed) to the database immediately.
Meanwhile other users changes have been flushed to the database and thats the reason the entries in the database are in different order..

Can you pls also tell me is there a way i can make sure the changes made while saving the object are flushed to the database immediately..


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 28, 2008 4:42 pm 
Newbie

Joined: Thu Jan 04, 2007 1:36 pm
Posts: 16
There are different ways to manage the flush mode. One is via hibernate property 'org.hibernate.flushmode' in your hibernate config file and you can set it to always or commit or manual. Or you can override the default behaviour at a specific method level by creating a query or criteria by setting the FlushMode parameter. But its generally better to leave the flush mode alone ("auto" mode).
In your case, I believe you may have X as a parent or a child of another element. And when X is persisted, it issues a create (with version 0) and issues an update (incrementing version to 1) to add the association to its parent or child. Hence, 2 sqls issued for the same instance and the reason behind version number being incremented.
Regarding the ordering of the records.. I think its just the db client you're using which by default is not ordering the ids.. Do you have a timestamp on the records as to when they were created? Im pretty sure record 105 would not have been created before record 104.

Regards,
Arun


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 28, 2008 10:15 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
bourne2hib wrote:
Can you pls also tell me is there a way i can make sure the changes made while saving the object are flushed to the database immediately..



You could call the flush right after save.



Farzad-


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