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: version field issue
PostPosted: Wed May 21, 2008 8:01 am 
Newbie

Joined: Wed May 21, 2008 6:57 am
Posts: 8
greetings!

i create an entity instance which is persisted and is given id and version number (zero it is). There are no other entities with same unique persistent identity in persistence context (naturally). Version field holds 0 value until em.flush() is called (flushing is controlled via seam conversations, and is MANUAL). Context is written to db, but that particular entity gets version value 1 (sometimes even StaleStateException is thrown and data is still written to db). What could cause such a behavior?

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 21, 2008 9:48 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
The StaleStateException is fairly consistent in why it is thrown:

Quote:
Thrown when a version number or timestamp check failed, indicating that the Session contained stale data (when using long transactions with versioning). Also occurs if we try delete or update a row that does not exist.

Note that this exception often indicates that the user failed to specify the correct unsaved-value strategy for a class!


http://www.hibernate.org/hib_docs/v3/api/org/hibernate/StaleStateException.html

My guess is that you might be updating or deleting records before they are even committed to the database. saveOrUpdate does not need to be called if the objects are already associated with the session. You may be issuing this call too often. See if the objects you are saving are indeed detached or transient objects, and not persistent objects under the spell of the Hibernate Session.

This tutorial on How Hibernate Works might be helpful.

http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=07howhibernateworks

-Cameron McKenzie

_________________
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: Wed May 21, 2008 9:58 am 
Newbie

Joined: Wed May 21, 2008 8:33 am
Posts: 5
Hi,
I think version value changes only when any update happens to the record. i.e. only when update query is fired.

First of all if you are not changing the value of a bean why do you need to call flush() method for it . I assume that you are calling flush() because some other object has changed.

Let us assume there is "User" entity with "company" column.
Let u1.comany="OsCommerce".

Whenver there is any change to u1.company field, Hibernate "marks" the change using "dirty flag" on the entity. At the time of flush a set of update statements will be run for all objects that are "dirty".
So you need to see if you are calling any setters on the object with the old values again set to the fields i.e. you call user.setCompany( "OsCommerce").
Here You are not actually changing the value. But still you are calling the setter method. I am not sure for such kind of setter invocations (where there is no change in the actual value), whether an update statement will be run or not. You need to enable show_Sql=true for hibernate and see what statements it is executing.
( What will happen if you call user.setCompany(new String("OsCommerce") )? I need to check )

What I suggest is to enable show_Sql=true in cfg.xml and see what statements are executed to understand whats going on.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 21, 2008 10:27 am 
Newbie

Joined: Wed May 21, 2008 6:57 am
Posts: 8
thanks for explaining "dirty flag"concept, but i'm not sure that's the case:

my entity is brand new, and after persist() was called upon it no further setter accesses its attributes. i have sql output turned on and it shows insert command for the entity (among many others) and then at the and update command where version gets incremented.


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.