-->
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: merge problem - version of returned instance not updated
PostPosted: Tue Feb 27, 2007 9:30 am 
Newbie

Joined: Tue Feb 27, 2007 7:56 am
Posts: 2
Location: Timisoara, Romania
Hi, we have a big problem regarding merge()

We use version property for optimisting locking
and we are saving detached objects using merge()

Because consecutive saves on the same detached objects can occur,
we're trying to keep the version updated, by reading it from the returned persistent instance of the same object.

This is supposed to be the current state of the object (as the api states), BUT it is not !!! The version of the returned instance it is the OLD one.
So when trying to save the object again we're getting a StaleObjectStateException

Hibernate version: 3.2.2.ga

Base DAO
We are using org.springframework.orm.hibernate3.support.HibernateDaoSupport
Code:
public abstract class AppBaseDAOHibernate<E extends BaseEntity>
        extends HibernateDaoSupport {}


Save method
Code:
public BaseEntity save(final E entity) {
    BaseEntity savedEntity = (BaseEntity) getSession().merge(entity);
    entity.setXversion(savedEntity.getXversion());
    return savedEntity;
}


Please help!

_________________
Cristi Toth
-----------
Codebeat


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 28, 2007 1:29 am 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi cristith,


always use merge in this way

public BaseEntity save(final E entity) {
BaseEntity savedEntity = (BaseEntity) getSession().merge(entity);
return savedEntity;
}


and it will change your ref .So you need to use returned ref.Merge use to merge your current intity to old entity.and old entity will remain in session and new entity will not attached .So using megre is programming cost.I would prefer avoid to use merge.

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 28, 2007 3:10 am 
Newbie

Joined: Tue Feb 27, 2007 7:56 am
Posts: 2
Location: Timisoara, Romania
Hi,

I don't need the new reference of the entity, I just need to know the VERSION property. The entity I'm trying to save has refs to some collections, so I wouldn't want to loose them.

The thing is the returned instance is identical to the one saved. It is not the one updated in the DB (so the version property is the old one).

_________________
Cristi Toth
-----------
Codebeat


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.