-->
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.  [ 3 posts ] 
Author Message
 Post subject: Unable to save objects under certian circumstances
PostPosted: Thu Sep 18, 2003 2:09 am 
Newbie

Joined: Wed Aug 27, 2003 1:37 am
Posts: 10
Location: Kyrgyzstan
Hello,

I have several objects that work fine in my test cases but are failing to work in my application. Here is the stack trace of the error I get when I save a GenericDrug

Caused by: net.sf.hibernate.HibernateException: Another object was associated with this id (the object with the given id was already loaded): [com.atwoodsoft.data.Language#0]
at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1294)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1218)
at net.sf.hibernate.engine.Cascades$3.cascade(Cascades.java:81)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:238)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:306)
at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1312)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1218)
at net.sf.hibernate.engine.Cascades$3.cascade(Cascades.java:81)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:238)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:272)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:306)
at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1312)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1218)
at net.sf.hibernate.engine.Cascades$3.cascade(Cascades.java:81)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:238)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:306)
at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1312)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1218)
at net.sf.hibernate.engine.Cascades$3.cascade(Cascades.java:81)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:238)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:306)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:708)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:620)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1214)
at com.atwoodsoft.persistence.datamanager.impl.HibernateDataManager.save(HibernateDataManager.java:53)
... 27 more

Here is the mapping of the relavent objects:

<class name="com.atwoodsoft.data.Language" table="LanguageTest">
<id name="id" type="int" column="id" unsaved-value="-1">
<generator class="native"/>
</id>
<property name="name" column="name" type="string" length="50" not-null="true"/>
<property name="localName" column="localName" type="string" length="50" not-null="true"/>
<property name="languageCode" column="languageCode" type="string" length="50" not-null="true"/>
</class>
<class name="com.atwoodsoft.data.MultilanguageString" table="MultilanguageString">
<id name="id" type="int" column="id" unsaved-value="-1">
<generator class="native"/>
</id>
<set name="localizedStrings" cascade="all" inverse="true">
<key column="stringCollection"/>
<one-to-many class="com.atwoodsoft.data.LocalizedString"/>
</set>
</class>
<class name="com.atwoodsoft.data.LocalizedString" table="LocalizedString">
<id name="id" type="int" column="id" unsaved-value="-1">
<generator class="native"/>
</id>
<property name="string" column="string" type="string" length="50" not-null="true"/>
<many-to-one name="language" class="com.atwoodsoft.data.Language" column="language"/>
<many-to-one name="stringCollection" class="com.atwoodsoft.data.MultilanguageString" column="stringCollection"/>
</class>
<class name="com.atwoodsoft.data.Catagory" table="Catagory">
<id name="id" type="int" column="id" unsaved-value="-1">
<generator class="native"/>
</id>
<many-to-one name="name" cascade="all" column="name"/>
<many-to-one name="description" cascade="all" column="description"/>
<set name="genericDrugs" cascade="all" inverse="true" lazy="false">
<key column="catagory"/>
<one-to-many class="com.atwoodsoft.data.GenericDrug"/>
</set>
</class>
<class name="com.atwoodsoft.data.GenericDrug" table="GenericDrug">
<id name="id" type="int" column="id" unsaved-value="-1">
<generator class="native"/>
</id>
<many-to-one name="name" cascade="all" column="name"/>
<many-to-one name="description" cascade="all" column="description"/>
<many-to-one name="catagory" cascade="all" class="com.atwoodsoft.data.Catagory" column="catagory"/>
<set name="products" cascade="all" inverse="true" lazy="false">
<key column="generic"/>
<one-to-many class="com.atwoodsoft.data.DrugProduct"/>
</set>
</class>

Any suggestions would be appreciated.

John


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 18, 2003 5:36 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Have a look at the advanced problems FAQ
http://www.hibernate.org/117.html#A19

Quote:
For a particular session, there may be at most one object that represents a particular database row. So, if you already loaded an object with a particular identifier, you can't then try to associate a different object with the same identifier by calling update().

If you absolutely must do this, evict() the original instance first - but it is much better to simply avoid this situation.


Top
 Profile  
 
 Post subject: What about transparent persistence then?
PostPosted: Fri Sep 19, 2003 12:39 am 
Newbie

Joined: Wed Aug 27, 2003 1:37 am
Posts: 10
Location: Kyrgyzstan
epbernard

Thank you very much. It does indead apear that Language object is loaded once when cascading to the name and description MultilanguageStrings of the GenericDrug and again when cascading to the name and description MultilanguageStrings of the Catagory object. This situation occurs frequently within my program. I was hoping that by using cascading updates I could make the persistence more transparent. I guess that this is not the case and I will need to work more on the mapping. Any suggestions regarding best practices for this type of case would be appreciated.

Thanks for your help,
John


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.