-->
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.  [ 7 posts ] 
Author Message
 Post subject: Implementing save method
PostPosted: Tue Feb 24, 2009 9:36 am 
Newbie

Joined: Tue Feb 24, 2009 8:20 am
Posts: 2
Hello,

I have a problem and searched already a lot to try to solve this. It has to do with a NonUniqueObjectException, and it appears to me that quite a few developers have problems with this type of exception.


I have an application which has some business objects and uses Hibernate to store these objects into a database (actually quite simple).

When a single object must be persisted I have no problems and Hibernate is quite straightforward, but as soon as I begin with bidirectional associations I ran into problems.

I'm getting a NonUniqueObjectException when trying to save one of my objects. I will illustrate this with a simplified example:

I have an object A. This object has a one-to-many relationship with object B. Object B has a one-to-many relationschip with object C. I made the settings in the mapping files for these objects that I only have to save object A, and object B and C will be automatically saved by a cascade save-update.

The error occurs as soon as I have following situation:

A --> B1 --> C
A --> B2 --> C

So in this case both B1 and B2 have an assocation with C.
When I call session.save(A) i'm getting the NonUniqueObjectException. Hibernate wants to save B1 and therefore reattaches C and saves this, then B2 must be saved so C must be reattached, but C is already attached and the exception is thrown.

Do I miss something? I would presume that Hibernate detects that C is already attached and therefore does not try this a 2nd time. For all these objects the equals() and hashCode() methods are implemented.

How can you avoid such problems?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 10:01 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Try to use session.merge().
Is A a new Object but C is detached? Maybe you could provide your code.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 3:49 pm 
Newbie

Joined: Tue Feb 24, 2009 8:20 am
Posts: 2
A is a new object, as is B1 and B2. C is a detached object.

When I just change session.save(A) to session.merge(A) gives me an exception:
object references an unsaved transient instance - save the transient instance before merging

Do I have to break up my save statement and save or merge all the A, B and C objects separately?

I'm just looking for a general approach to avoid such problems?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2009 4:29 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
In that case I would probably remove CascadeType persist (or save-update) from relation B-->C and update C in another statement.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2009 4:31 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
In that case I would probably remove CascadeType persist (os save-update) from relation B-->C and update C in another statement.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2009 7:05 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
I am getting a feeling tht the issue is with ur mapping or data. Can you post the relevant mapping?

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject: Re: Implementing save method
PostPosted: Wed May 20, 2009 7:20 pm 
Beginner
Beginner

Joined: Tue Feb 03, 2009 4:33 pm
Posts: 21
Mynca, did you ever find a solution to this?


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