-->
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.  [ 6 posts ] 
Author Message
 Post subject: Correct way to update object in long transaction?
PostPosted: Sun Dec 07, 2003 10:07 pm 
Newbie

Joined: Fri Dec 05, 2003 12:44 am
Posts: 16
I'm having a lot of trouble working out what is the correct way to do updates in the context of long transactions.

Given the following pseudo code. I would expect that the second saveOrUpdate should throw an exception given that the mapping for class Foo uses a version.

Code:
Foo foo1;
Foo foo2;

s = sessionFactory.openSession();

foo1 s.load(Foo.class, new Integer(1));

s.close();

.. do some work

s = sessionFactory.openSession();

foo2 s.load(Foo.class, new Integer(1));

s.close();

... do some work which modifies foo1

s = sessionFactory.openSession();

s.saveOrUpdate(foo1);

s.close();

... do some work which modifies foo2

s = sessionFactory.openSession();

s.saveOrUpdate(foo2);

s.close();



My first thought was that perhaps I need to reassociate the Foo objects with the session using s.lock(foo, LockMode.READ) but if I do that I get a
Code:
net.sf.hibernate.HibernateException: reassociated object has dirty collection reference
at the first update.

Any advice would be greatly appreciated.

Thanks

Oliver Hutchison


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 07, 2003 10:38 pm 
Newbie

Joined: Fri Dec 05, 2003 12:44 am
Posts: 16
Quote:
at the first update.


that should have been "at the first lock."


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 1:16 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
I would expect that the second saveOrUpdate should throw an exception given that the mapping for class Foo uses a version.


it does.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 1:23 am 
Newbie

Joined: Fri Dec 05, 2003 12:44 am
Posts: 16
Quote:
it does.


not for me... I must have something wrong with my mapping. Also could you please let me know what the exception that should be throws is and fom where you'd expect it would be thrown.

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

    <class name="ourcommunity.giving.dao.AppealDO" table="Appeal"
            optimistic-lock="version">

        <cache usage="read-write" />
   
        <id name="id" type="int" column="ID" unsaved-value="0" >
            <generator class="identity"/>
        </id>
       
        <version name="version" type="int" />
       
        <property name="name" column="Name" />       
        <property name="enabled" column="Enabled" />
        <property name="generalStatement" column="GeneralStatement" />
        <property name="image" column="Image" />
        <property name="expenditure1" column="Expenditure1" />
        <property name="expenditure2" column="Expenditure2" />
        <property name="expenditure3" column="Expenditure3" />
        <property name="customHeading1" column="CustomHeading1" />
        <property name="customHeading2" column="CustomHeading2" />
        <property name="customHeading3" column="CustomHeading3" />
        <property name="customText1" column="CustomText1" />
        <property name="customText2" column="CustomText2" />
        <property name="customText3" column="CustomText3" />
        <property name="furtherDetails" column="FurtherDetails" />
        <property name="customLink" column="CustomLink" />
        <property name="emailText" column="EmailText"/>

        <set name="questions" cascade="all">
            <cache usage="read-write" />
            <key column="AppealID"/>
            <one-to-many class="ourcommunity.giving.dao.QuestionDO"/>
        </set>
           
        <property name="isTaxDeductible" column="IsTaxDeductible" />
        <property name="isReceiptProvided" column="IsReceiptProvided" />
        <property name="displayBanner" column="DisplayBanner" />
        <property name="expiryDate" column="ExpiryDate" type="timestamp" />
       
        <many-to-one name="contact" column="ContactID" cascade="all" unique="true"
            class="ourcommunity.contact.dao.ContactDO" />
           
        <property name="orgId" column="OrgID" />
       
       
    </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 5:36 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
net.sf.hibernate.StaleObjectStateException

You forgot to either or both:
- commit your tx
- flush your session

see section 8.9 of the ref guide.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 8:05 pm 
Newbie

Joined: Fri Dec 05, 2003 12:44 am
Posts: 16
Actually the problem was related to my using Session.saveOrUpdateCopy instead of Session.saveOrUpdate.

I was a little bit confused about what exactly the difference between the two was.


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