-->
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: How do you persist an object with ref to existing record??
PostPosted: Wed Mar 18, 2009 4:09 pm 
Newbie

Joined: Mon Mar 09, 2009 5:23 am
Posts: 3
Hibernate version: 3.2.3.ga

Hi,

Im relatively new to Hibernate but have poured through the documentation today and still drawing blanks so any help much appreciated.

Im getting this exception since "NonUniqueObjectException: a different object with the same identifier value was already associated with the session".

Im trying to save an object eg. TaggedArticle which has a nested Tag object. I dont want duplicate Tag objects in the Tag table so I do a check first to see if the Tag exists and if it does exist I get its ID and set it on the Tag nested in the TaggedArticle I am about to try and save.


TaggedArticle
|
Tag (set ID of already existing record on here)


then I try and persist as so:


Serializable id = session.save(taggedArticle);




My question is - how do I persist an object eg . TaggedArticle with the ID of an exisitng Tag record?


<hibernate-mapping>
<class name="com.TaggedResult" table="TAGGED_RESULT" >
<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="native" />
</id>
<property name="userId" type="java.lang.Integer">
<column name="USER_ID" not-null="true" />
</property>
<many-to-one name="tag"
class="com.Tag" cascade="save-update" unique="true">
<column name="TAG_ID" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>


<hibernate-mapping>
<class name="com.Tag" table="TAG" >
<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="native" />
</id>
<property name="tag" type="java.lang.String" >
<column name="TAG" length="40" not-null="true" />
</property>
<property name="createdDate" type="java.util.Date">
<column name="DATE_CREATED" length="8" />
</property>
<property name="updatedDate" type="java.util.Date">
<column name="DATE_UPDATED" length="8" />
</property>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 19, 2009 3:41 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
...so I do a check first to see if the Tag exists and if it does exist I get its ID and set it on the Tag nested in the TaggedArticle...


I guess this is the problem. You are not allowed to have two different Tag objects with the same ID. Since you did a check this probably means that Hibernate has already loaded a Tag object with the specific id already. What you need to do is to change the Tag object that the TaggedArticle references to the already existing Tag object. Eg something like:

Code:
TaggedArticle article = ...
Tag existing = (Tag)session.get(Tag.class, tagId);
article.setTag(existing);


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.