tags:

views:

2072

answers:

1

Hello All

My question is

How can I get new version number of the object after calling saveOrUpdate method?

Please check following scenario

class ServiceInfo{
   int serviceId;
   Set<InfoItem> infos;   
   int version;
}

class InfoItem{
   int serviceId
   some Properties...
}

Mapping file for ServiceInfo

 <version column="VERSION" name="version" type="integer" unsaved-value="null" insert="true" />

<set name="infos" inverse="true" lazy="true" cascade="all">
            <key column="serviceId" not-null="true"/>
            <one-to-many class="myPackage.InfoItem"/>
 </set>

Whenever an InfoItem is added or removed for a specific ServiceInfo, I'm saving serviceInfo instance by calling saveOrUpdate method. This method inserts all of new InfoItems in ServiceInfo to db successfully.

Generic DAO example at http://www.hibernate.org/328.html shows makePersistent method like following

@SuppressWarnings("unchecked")
    public T makePersistent(T entity) {
        getSession().saveOrUpdate(entity);
        return entity;
    }

After first save, user can continue processing on ServiceInfo and he/she might want to save it again. (adding and deleting InfoItem) At that point, in order not to get Optimistic lock exception, I want to keep ServiceInfo instance with latest version number in the user session(not hibernate session).

Calling saveOrUpdate inserts new version number to db but it doesn't update on T instance in this case ServiceInfo.... Why???

And I can not call refresh method like below

   @SuppressWarnings("unchecked")
    public T makePersistent(T entity) {
        getSession().saveOrUpdate(entity);
        getSession().refresh(entity);
        return entity;
    }

Because new InfoItem is not in the db yet as the transaction is not commited. So I will be getting HibernateException.

Any suggestion?

+1  A: 

The problem was not a problem in my case... Version info get updated properly...

glad you figured out whatever it was :D hehe.
Arthur Thomas