-->
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.  [ 9 posts ] 
Author Message
 Post subject: how to avoid NonUniqueObjectException?
PostPosted: Sun Feb 05, 2006 7:37 am 
Newbie

Joined: Tue Nov 22, 2005 10:08 am
Posts: 17
Hi

Imagine the scenario with standard one-to-one mapping, say Result has one Transaction (and one Transaction can be shared by more Results). Mapping has cascading turned on (cascade="save-update").

Saving (making the object persistent) Result instance cascades saving Transaction.

session.save(result)

But when it comes to saving the result among the same session I get NonUniqueObjectException.

//operation 1
result.setTransaction(sameTransaction);
session.save(result);

//operation 2
result2.setTransaction(sameTransaction);
session.save(result); ->> NonUniqueObjectException - Transaction

This code works properly when both operations are in different sessions.

Any suggestions how to solve it in an ellegant way (that is not: checking if transaction is already in session, then merge it with current transaction, then set merged transaction in result)?


Top
 Profile  
 
 Post subject: Use saveOrUpdate()
PostPosted: Sun Feb 05, 2006 8:08 am 
Newbie

Joined: Thu Jan 05, 2006 7:33 pm
Posts: 13
When an object is in persistant state (is associated with a Session object) no other objects that represent the same row can be associated with that Session.

And of course, by saving the "Transaction" entity in the first call, it will be associated with the Session in the second call, causing the exception.

Solution:

use the Session.saveOrUpdate(YourEntityInstance) method. It checks if the entity is associated with the Session and if so performs an update(), rather than a save() on it.

have a look at this part of the docs

http://www.hibernate.org/hib_docs/refer ... g-detached

cheers

Michael


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 05, 2006 8:53 am 
Newbie

Joined: Tue Nov 22, 2005 10:08 am
Posts: 17
Thank for a quick response, but...

Docs:
http://www.hibernate.org/hib_docs/refer ... g-detached

saveOrUpdate() does the following:
...
if another object associated with the session has the same identifier, throw an exception
...

So obviously I still get this error.

It seems that I can use saveOrUpdateCopy() to shun the NonUniqueObjectException, but this method has been deprecated and replaced by merge(), which in fact works differently ("the given instance does not become associated with the session, it remains detached")

Any ideas?


Last edited by szczepiq on Sun Feb 05, 2006 10:13 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 05, 2006 9:47 am 
Newbie

Joined: Thu Dec 22, 2005 11:15 pm
Posts: 13
I have the same problem as following:

Code:
Parent Object:  Class Parent parent;
Child Object: Class Child child;
Parent and Child has one-to-many relationship, where Parent's children collection has the property "cascade=all".

I do the following:

Code:
Transaction tx = session.beginTransaction();
Parent parent = new Parent();
session.save(parent);-------------1

Child child = new Child();
parent.addChild(child);
session.save(parent);---------------2

tx.commit();

Sometimes, not always, statement2 throws org.hibernate.NonUniqueObjectException.

Eargerly want to know how to avoid this.

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 05, 2006 10:11 am 
Newbie

Joined: Tue Nov 22, 2005 10:08 am
Posts: 17
In your case, NonUniqueObjectException may concern Child or Parent object

Assuming that in both statements parents are different objects (I mean different id, or both id=0, so that hibernate will generate one), perhaps you attach same child to both parents?

And of course NonUniqueObjectException concerns only single transaction. If you consider putting operations on parents (save(parent)) into different transactions, you won't ever get one. Yes, I know that this is not desirable to create more transactions, not mentioning that this is far less intuitive.

Let's wait for some Hibernate gurus
. Perhaps they will show us how to avoid NonUniqueObjectExceptions.

Thanks,
Szczepan


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 05, 2006 10:46 am 
Newbie

Joined: Tue Nov 22, 2005 10:08 am
Posts: 17
I think there are 2 solutions:
- catch NonUniqueObjectException, perform merge(), then save
- separate transactions

Correct?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 05, 2006 10:48 am 
Newbie

Joined: Thu Dec 22, 2005 11:15 pm
Posts: 13
[quote="szczepiq"]Assuming that in both statements parents are different objects (I mean different id, or both id=0, so that hibernate will generate one), perhaps you attach same child to both parents?

[b]

In my problem, parent in the two statement above is the same object.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 13, 2006 1:03 pm 
Newbie

Joined: Thu Jun 02, 2005 8:23 am
Posts: 3
Location: Belgium - Brussels
I notice in one of the posts above sometimes it happens, sometimes not. Probably you changed a property (field) of the object you saved in the first 'save', that changed your isEqual() outcome.

The ID of the object has not not changed, but the outcome of the isEqual method has. So when the changed object is compared to the one that is in the session, they will have the same ID but are not equal according to the isEqual method, resulting in NonUnique exception.

What you could do is just before the second saveOrUpdate, do an evict of the object in question.

Hope this helps,

Steven


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 8:28 am 
Newbie

Joined: Wed May 31, 2006 8:58 am
Posts: 6
Location: Netherlands
I got this problem on my application aswell.

i changed the cascade type to "merge" in the mapping files.
eg. the hibernate-mapping tag, set default-cascade="merge"

this worked with hibernate 3 mappings.


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