-->
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.  [ 8 posts ] 
Author Message
 Post subject: Updating problem
PostPosted: Wed May 07, 2008 6:22 pm 
Newbie

Joined: Fri Apr 18, 2008 1:07 am
Posts: 14
Location: Pakistan
Hi All!

I am having problem with updating the records. When I try to call update or saveOrUpdate function, hibernate exception raises with this message

a different object with the same identifier value was already associated with the session

I am very much concerned with the problem. Please somebody help me. Thanks in advance

_________________
Muhammad Kashif Nazar
Software Engineer


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 08, 2008 12:54 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
That is because you are trying to update a detached object. You could do a merge() before update. Or even better, update the object in the session instead of doing a detached object update.

_________________
Sukirtha


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 08, 2008 1:09 am 
Beginner
Beginner

Joined: Wed Mar 05, 2008 4:57 am
Posts: 22
Location: Bangalore,India
Please provide the xml mapping and the java code wgere u r updating the entitiy.

_________________
Naresh Waswani
+91-9986461501


Top
 Profile  
 
 Post subject: Attached and Detached objects
PostPosted: Thu May 08, 2008 4:18 pm 
Newbie

Joined: Fri Apr 18, 2008 1:07 am
Posts: 14
Location: Pakistan
Sukirtha wrote:
That is because you are trying to update a detached object. You could do a merge() before update. Or even better, update the object in the session instead of doing a detached object update.


This is the body of the update function in the DAO class

org.hibernate.Transaction tx=getSession().beginTransaction();
getSession().update(instance);
tx.commit();

What does it mean by detaching objects and attaching objects?

_________________
Muhammad Kashif Nazar
Software Engineer


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 08, 2008 4:24 pm 
Newbie

Joined: Fri Apr 18, 2008 1:07 am
Posts: 14
Location: Pakistan
waswani wrote:
Please provide the xml mapping and the java code wgere u r updating the entitiy.


Code:
<hibernate-mapping>
    <class name="lacarte.persistence.User" table="user" catalog="lacarte">
        <id name="userId" type="java.lang.Integer">
            <column name="userId" />
            <generator class="increment" />
        </id>
        <many-to-one name="company" class="lacarte.persistence.Company" fetch="select">
            <column name="companyId" />
        </many-to-one>
        <property name="userName" type="java.lang.String">
            <column name="userName" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="password" />
        </property>
        <property name="firstName" type="java.lang.String">
            <column name="firstName" />
        </property>
        <property name="lastName" type="java.lang.String">
            <column name="lastName" />
        </property>
        <property name="address" type="java.lang.String">
            <column name="address" />
        </property>
        <property name="zip" type="java.lang.String">
            <column name="zip" />
        </property>
        <property name="city" type="java.lang.String">
            <column name="city" />
        </property>
        <property name="state" type="java.lang.String">
            <column name="state" />
        </property>
        <property name="country" type="java.lang.String">
            <column name="country" />
        </property>
        <property name="type" type="java.lang.Integer">
            <column name="type" />
        </property>
        <property name="phone" type="java.lang.String">
            <column name="phone" />
        </property>
        <set name="companies" inverse="true">
            <key>
                <column name="userId" />
            </key>
            <one-to-many class="lacarte.persistence.Company" />
        </set>
        <set name="transactions" inverse="true">
            <key>
                <column name="userId" />
            </key>
            <one-to-many class="lacarte.persistence.Transaction" />
        </set>
    </class>
</hibernate-mapping>



This is my mapping file for the User Bean

_________________
Muhammad Kashif Nazar
Software Engineer


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 09, 2008 12:24 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
Quote:
What does it mean by detaching objects and attaching objects?


I am sure you are doing a retrieve before you start modifying the object. But somewhere in between the retrieve and update() method call, you are closing the session and re-opening it for update. Once a session closes, the retrieved object becomes detached. There are ways to handle detached object update. One such way is merge() instead of saveOrUpdate() or update().

Refer Hibernate_reference.pdf section 10.6
http://www.hibernate.org/hib_docs/reference/en/pdf/hibernate_reference.pdf

_________________
Sukirtha


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 09, 2008 4:51 am 
Newbie

Joined: Fri Apr 18, 2008 1:07 am
Posts: 14
Location: Pakistan
Quote:
I am sure you are doing a retrieve before you start modifying the object. But somewhere in between the retrieve and update() method call, you are closing the session and re-opening it for update. Once a session closes, the retrieved object becomes detached. There are ways to handle detached object update. One such way is merge() instead of saveOrUpdate() or update().

Refer Hibernate_reference.pdf section 10.6
http://www.hibernate.org/hib_docs/reference/en/pdf/hibernate_reference.pdf
[/quote]

There is another problem when I use merge(). I lose the associations when I merge() the object.

_________________
Muhammad Kashif Nazar
Software Engineer


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 09, 2008 5:52 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
Quote:
There is another problem when I use merge(). I lose the associations when I merge() the object.


I guess that is because you have not explicitly asked to cascade the relationships during a merge/save-update operations.

include cascade="merge, save-update" in <set> collections in your mapping file.

i.e.,

Code:
        <set name="companies" inverse="true" cascade="merge, save-update">         
        <key>
               <column name="userId" />
         </key>
            <one-to-many class="lacarte.persistence.Company" />
        </set>
        <set name="transactions" inverse="true" cascade="merge,save-update">
            <key>
                <column name="userId" />
            </key>
            <one-to-many class="lacarte.persistence.Transaction" />
        </set>

_________________
Sukirtha


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