-->
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.  [ 5 posts ] 
Author Message
 Post subject: Possible solutions for NonUniqueObjectException
PostPosted: Mon Aug 23, 2004 10:54 am 
Newbie

Joined: Wed Mar 24, 2004 6:18 am
Posts: 15
Hi! I've had some problems recently with NonUniqueObjectException's (a different object with the same identifier value was already associated with the session). The problem is that I'm using the method copyProperties of class BeanUtils to perform the conversion "persistent object->bean->persistent object". After executing this method, the object identifier of the nested objects is lost (I'm using converters, which create new instances, to convert nested objects) and when I try to update a persistent object I get the aforementioned exception.

I have a workaround for this but it's not generic nor "elegant", so I'd like to know how do you manage this situation.

Thanks in advance,
Diego


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 23, 2004 11:05 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
http://forum.hibernate.org/search.php?mode=results&search_keywords=NonUniqueObjectException

_________________
Leonid Shlyapnikov


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 23, 2004 11:12 am 
Newbie

Joined: Wed Mar 24, 2004 6:18 am
Posts: 15
Thanks, but I had already searched the forum, and I really think I've read all the messages about this topic.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 23, 2004 11:28 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
diegoman wrote:
Thanks, but I had already searched the forum, and I really think I've read all the messages about this topic.

I don't think anybody could help... I don't see your mappings and java code.
I could only suggest you to avoid the following practice (but if you have searched the forum you should know this):
Code:
obj = (MyClass) session.get(id);
obj.setProperty1(value); // modify the object
//session.update(obj) // DON'T  DO THIS, the obj is already associated with the session and should be saved/update

_________________
Leonid Shlyapnikov


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 23, 2004 11:56 am 
Newbie

Joined: Wed Mar 24, 2004 6:18 am
Posts: 15
OK, I didn't put code because I thought it wasn't neccessary in this case, but here it is:

<hibernate-mapping>
<class name="MyClass" table="my_class">
<id
name="id"
type="java.lang.Long"
column="id">
<generator class="native" />
</id>
<property
name="oneProperty"
type="java.lang.String"
column="one_property"
length="10"/>
<many-to-one
name="nestedObject"
class="NestedObject"
not-null="true"
cascade="all">
<column name="id_nested_object" />
</many-to-one>
</class>
</hibernate-mapping>

<hibernate-mapping>
<class name="NestedObject" table="nested_object">
<id
name="id"
type="java.lang.Long"
column="id">
<generator class="native" />
</id>
<property
name="anotherProperty"
type="java.lang.String"
column="another_property"
length="10"/>
</class>
</hibernate-mapping>

Java code:

myObject = ses.load(myClass, some_id);
BeanUtils.copyProperties(myBean,myObject);
myBean.setOneProperty('mec');
myBean.getNestedObject().setAnotherProperty('mic');
BeanUtils.copyProperties(myObject,myBean);//PROBLEM: here, a new Instance of nestedObject has been created; the old reference it's been lost
ses.saveOrUpdate(myObject); //NonUniqueObjectException on nestedObject; tried with saveOrUpdateCopy but didn't work because of this, I think: http://forum.hibernate.org/viewtopic.ph ... texception


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