-->
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.  [ 5 posts ] 
Author Message
 Post subject: version is not incremented after merge
PostPosted: Tue Oct 30, 2007 7:09 am 
Newbie

Joined: Tue Oct 30, 2007 6:19 am
Posts: 2
Hibernate version: 3.2.5.GA with em 3.3.1.GA

This is the base class for our persistent objects:

Code:
/**
* Base class for persistent objects.
*
* @actionscript.class bindable=true
*/
@MappedSuperclass
public abstract class EntityObject implements Serializable {
    @Id
    @GeneratedValue
    private long id;

    @Version
    @Column(nullable = false)
    private Long version;

[...]
}


when calling em.merge(object) the version property is not incremented in the entity object (it is in the database), hence when i reuse and merge the same object a second time i get an optimistic locking exception. even worse, if i try to reload the object after merging via refresh and find, i still get the object with the old version id.

i stepped through the code and it seems, that the copy of the entity, that is put into the session cache, is made before the fields are updated with the values from the database.

maybe related to this issue is a mysql issue with timestamp version properties. mysql only has a granularity of seconds for timestamps, hence the version timestamp gets truncated when the object is persisted into the database. since the resulting truncated value is never updated in the entity any further operation fails.

can this be considered to be a bug in the entity manager or am i doing something wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 30, 2007 2:03 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
No you are misusing the API.
object2 = em.merge(object1);

you should use object2, which is the managed copy attached to the persistence context and discard object1

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 31, 2007 7:29 am 
Newbie

Joined: Tue Oct 30, 2007 6:19 am
Posts: 2
which isn't very helpful at all. what if object1 is part of some many-to-many relations? i would have to replace it in the other objects collections after storing it otherwise i either get duplicate entries (persist) or optimistic locking exceptions (merge).

this is a weird approach.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 31, 2007 5:12 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
no it's not. The whole object graph is merged (if you use cascading strategy accordingly). You just need to merge through the root object (or any object that can reach the data you want)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 31, 2007 5:12 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
That being said, I prefer the saveOrUpdate semantic.

_________________
Emmanuel


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