-->
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.  [ 2 posts ] 
Author Message
 Post subject: saveOrUpdate - Cascade - Automatic Versioning
PostPosted: Thu Sep 25, 2008 7:11 am 
Newbie

Joined: Thu Sep 25, 2008 6:03 am
Posts: 2
Hello All

My question is

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

Please check following scenario

Code:
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

Code:
   @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
Code:
   @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?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2008 7:53 am 
Newbie

Joined: Thu Sep 25, 2008 6:03 am
Posts: 2
Please ignore this question....

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


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