-->
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: NonUniqueObjectException
PostPosted: Sat Oct 16, 2004 8:12 pm 
Newbie

Joined: Sat Oct 16, 2004 7:58 pm
Posts: 8
Hibernate version:2.1.6

I have a class "Address" with one object of the class "Street", also i have a class "Person" who have a list of addresses. Two addresses may reference the same street.

The address need to do cascade save-update the street, and the person needs to cascade the addresses.

I have a person with one address referencing a street, when i try to save a new address for that person using the same street as the first one i get:

NonUniqueObjectException.

I just solved this by taking out the cascade and saving manually the streets, but i don't want to do it in that way.

Thanks, k...


Top
 Profile  
 
 Post subject: Similar...
PostPosted: Sat Oct 16, 2004 9:04 pm 
Newbie

Joined: Sat Oct 16, 2004 4:51 pm
Posts: 9
looks like we have similar issue but different error message due to different approach

read http://forum.hibernate.org/viewtopic.php?t=935398


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 16, 2004 9:35 pm 
Newbie

Joined: Sat Oct 16, 2004 7:58 pm
Posts: 8
i just read your problem, no idea of the solution but it doesn't seems to have a relation with my problem.

My problem is that the same object is associated twice in the same session.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 17, 2004 1:59 am 
Newbie

Joined: Sat Oct 16, 2004 4:51 pm
Posts: 9
Okay, please correct me if I miss something....

when you save address and person, are you tring to create two different records in database for street? or you are using common key for street record as FK in address and person?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 17, 2004 5:28 am 
Newbie

Joined: Sat Oct 16, 2004 7:58 pm
Posts: 8
The street is already saved in the db, and is the same for both addresses, the problem is that isn't the same object because one is the one that came with the person and the second is a new address with a loaded street.

I'm not trying to have two equal streets, i'm trying to make two addresses point to the same street, but because the first street is loaded when i load the person, and the second is loaded when i make the address(not in the same session) hibernate take them as 2 different objects... (i didn't even change anything in the street).


What i did maybe can help you.. I just is take off the cascade and save the street by myself when it was new, so it just take the relation attribute but don't try to insert-update the street.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 17, 2004 5:30 am 
Newbie

Joined: Sat Oct 16, 2004 7:58 pm
Posts: 8
Don't pay attention to the last part of the sentence "but don't try to insert-update the street." I just press Ctrl-z by mistake..

Sorry.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 17, 2004 11:14 am 
Newbie

Joined: Sat Oct 16, 2004 4:51 pm
Posts: 9
SO.... you removed cascade option from Hibernate mapping file and saved each object individually..... that means in my problem....

I need to alter contact.hbm.xml
<many-to-one name="AddressObject" column="ADDRESSKEY" class="data.Address" cascade="save-update"/> to
<many-to-one name="AddressObject" column="ADDRESSKEY" class="data.Address"/>

save address data first then get the addresskey and store it with contact object ... i.e. removing relations from hibernate mapping file and establish relation in programming logic, correct?!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 17, 2004 2:33 pm 
Newbie

Joined: Sat Oct 16, 2004 7:58 pm
Posts: 8
in the xml i just put cascade="none", i guess it's the same as don't use it.. but just in case.

What i do is save the street, then get the street object inside the address and save the address, in your case will be something like:

session.save(address);
contact.setAddress(address);
session.save(contact);

Don't worry about the key.. it will be in the address object after saving...


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.