-->
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.  [ 24 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Please Help. Another object was associated with this id ...
PostPosted: Wed Nov 12, 2003 10:45 am 
Pro
Pro

Joined: Mon Sep 08, 2003 4:30 pm
Posts: 203
H all,

I am getting the exception:
Another object was associated with this id (the object with the given id was already loaded)

(I am trying to do an update.)

My question is:

For one open session if I have 2 different class objects (say class A and class B) could it be possible to get the exception if an A object has the same id (primary key) like a B object?

This would be very bad, for I might have object A with id=1 and object B with id=1, but they are different objects/classes/tables !?

TIA,

--steve p.


Top
 Profile  
 
 Post subject: Re: Please Help. Another object was associated with this id
PostPosted: Wed Nov 12, 2003 11:22 am 
Regular
Regular

Joined: Mon Sep 08, 2003 4:53 am
Posts: 70
Location: Germany
dia100 wrote:
For one open session if I have 2 different class objects (say class A and class B) could it be possible to get the exception if an A object has the same id (primary key) like a B object?


No, this should not be the reason. You can have objects from different classes with the same id (primary key).

There has to be another reason for this exception. Maybe you are handling an object, that was loaded from another session with the same id?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2003 11:29 am 
Beginner
Beginner

Joined: Mon Sep 29, 2003 3:10 pm
Posts: 36
No, obviously, Hibernate will not throw this Exception just because two different types of objects have the same ID.

I don't know how helpful it will be to you but we recently ran across this exception in the following situation:

We were using Hibernate to load a list of objects. Within the same session we were later attempting to save an object that we had created ourselves (i.e. not loaded by Hibernate) of the same type with the same ID. Saving or updating an object attempts to place that object into the session, so naturally, since we had already loaded an object of the same type with the same ID, we got the the "already loaded" Exception, which was exactly what was happening.

I suggest taking this Exception at face value and look for other situations in the same session where you are manipulating that object and seeing if it's being put into the session before you attempt your update (which will attempt to put it in the session again).

-Matt


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2003 11:34 am 
Pro
Pro

Joined: Mon Sep 08, 2003 4:30 pm
Posts: 203
Thanks to all for the replies!

Actually I *did* re-create an object with an existing id in the same session :(

This is because :
1. I have a long lived session ( need for laziness )
2. it is easier to recreate an object, assign an existing id and try to update the new one instead of working with the original object, traverse object's graph (complicated in my case) in order to modify a specific property...

Any better suggestions?

TIA,

--steve p.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2003 11:38 am 
Regular
Regular

Joined: Mon Sep 08, 2003 4:53 am
Posts: 70
Location: Germany
What about using session.evict(object), to remove the object from the session, before "recreating" the object?

Or use session.refresh(object) ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2003 11:54 am 
Pro
Pro

Joined: Mon Sep 08, 2003 4:30 pm
Posts: 203
Quote:
What about using session.evict(object), to remove the object from the session, before "recreating" the object?


I have separated the business layer (BL) from the persistence layer (PL).
I wouldn't 'litter' the BL with db stuff, such as session.evict() before calling update in the PL.

Quote:
Or use session.refresh(object) ?


This would overwrite my newly modified object, which is not desired.

Other thoughts ?
Thx much!

--steve p.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2003 12:02 pm 
Regular
Regular

Joined: Mon Sep 08, 2003 4:53 am
Posts: 70
Location: Germany
What about a session.clear() (Hibernate 2.1) after every session.commit()?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2003 12:23 pm 
Pro
Pro

Joined: Mon Sep 08, 2003 4:30 pm
Posts: 203
andreas wrote:
What about a session.clear() (Hibernate 2.1) after every session.commit()?


I think this would work but I *only* commit on updates/saves so it would only for the very first update it won't take effect.

I think the easiest way to do is to implement in my persistence layer an evict method that has to be called from the business layer before attempting an update with modified objects.

thx,

--steve p.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2003 2:42 pm 
Pro
Pro

Joined: Mon Sep 08, 2003 4:30 pm
Posts: 203
I'm stunned !

Scenario:

1. open a session
2. read an object A of class C
3. close session

4. open a new session
5. create a new object B of class C
6. assign to B same identifier as A
7. try to update B

and get:
Another object was associated with this id (the object with the given id was already loaded)

Why? Because I have 2 different sessions???

TIA,
--steve


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2003 3:42 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
using cache ?
show the simplest code reproducing that.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2003 4:22 pm 
Pro
Pro

Joined: Mon Sep 08, 2003 4:30 pm
Posts: 203
epbernard wrote:
using cache ?
show the simplest code reproducing that.


no cahe used.
This is the code:

Code:
      Configuration cfg = new Configuration();
      sessionFactory = cfg.configure().buildSessionFactory();
      Session sess = sessionFactory.openSession();
      
      NetPortalCustomer user = null;
      List result = null;
      try {
         Query q =
            sess.createQuery(
               "from necustomer in class com.db.device.NetPortalCustomer where necustomer.name = :name");
         q.setString("name", "sherban");
         result = q.list();
         if (result.size() == 0)
            throw new ObjNotFoundException();
         user = (NetPortalCustomer) result.get(0);
      } catch (HibernateException e) {
         throw new DAOException(e);
      } finally {
         sess.close();
      }

      ContactInformation contactInfo = new ContactInformation();
      contactInfo.setName("ci");

      if (user.getContactInfo() != null) {
         contactInfo.setId(user.getContactInfo().getId());
      }
      
      user.setContactInfo(contactInfo);

      sess = sessionFactory.openSession();
      try {
         Transaction tx = sess.beginTransaction();
         sess.update(user);
         tx.commit();
      } catch (HibernateException e) {
         throw new DAOException(e);
      } finally {
         sess.close();
      }


and the mappings:

<joined-subclass name="com.diatem.db.device.NetPortalCustomer" table="necustomer">
<key column="id"/>
<property name="mgmtUserName" column="mgmt_user_name" type="string"/>
<property name="mgmtPassword" column="mgmt_passwd" type="string"/>
<property name="mgmtAccess" column="mgmt_access" type="boolean"/>
<many-to-one name="contactInfo" class="com.diatem.db.device.ContactInformation" cascade="all"/>
<many-to-one name="locationInfo" class="com.diatem.db.device.LocationInformation" cascade="all"/>

<set name="srvContractInfoList" cascade="all-delete-orphan" inverse="true" lazy="false">
<key column="customer"/>
<one-to-many class="com.diatem.db.service.SrvContractInfo"/>
</set>

</joined-subclass>

and

<class name="com.diatem.db.device.ContactInformation" table="contactinfo" mutable="true" polymorphism="implicit" dynamic-update="false" dynamic-insert="false">
<id name="id" type="long" unsaved-value="0">
<generator class="native">
</generator>
</id>

<property name="name" column="name" type="string" not-null="true" unique="false"/>
<property name="salutation" column="salutation" type="string"/>
<property name="phoneNumber" column="phone_number" type="string"/>
<property name="mobilePhone" column="mobile_phone" type="string"/>
<property name="pagerNumber" column="pager_number" type="string"/>
<property name="email" column="email" type="string" not-null="true"/>


</class>

please help ...

TIA,
--steve p.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2003 4:25 pm 
Pro
Pro

Joined: Mon Sep 08, 2003 4:30 pm
Posts: 203
and the error:

Another object was associated with this id (the object with the given id was already loaded): [com.diatem.db.device.ContactInformation#176] wraps: [net.sf.hibernate.HibernateException: Another object was associated with this id (the object with the given id was already loaded): [com.diatem.db.device.ContactInformation#176]]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2003 7:52 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Works for me on a slightly a simplified version of your mapping, with same code on hibernate 2.1beta4, HSQLDB
You should simplify it to isolate the issue.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2003 12:43 pm 
Pro
Pro

Joined: Mon Sep 08, 2003 4:30 pm
Posts: 203
I think I found the problem:

Scenario:
-Class Customer has a collection of Sites mapped cascade="all-delete-orphan"
-Class Site has a many-to-one with ContactInfo mapped cascade="all"
-Class Customer has itself a many-to-one with ContactInfo mapped cascade="all"
-Both Customer and Site point to the same ContactInfo instance (same FK values in both tables for Customer and Site)

Now, when I try to update Customer, I *believe* the session tries to load the same ContactInfo instance twice, one time for Customer and again for the Site. And then it gives the exception.

Any thoughts?

TIA,

--steve p.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2003 12:44 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
No. Hibernate of course looks in the session cache before loading anything.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 24 posts ]  Go to page 1, 2  Next

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.