-->
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: org.hibernate.NonUniqueObjectException : Simple use case...
PostPosted: Mon Oct 02, 2006 5:51 pm 
Beginner
Beginner

Joined: Fri Sep 16, 2005 7:51 am
Posts: 25
Hi there,

I have a relatively simple use case but am still jammed with a org.hibernate.NonUniqueObjectException.

I have an instance of an entity of class 'A', namely 'a'.

Another instance of another entity, class 'B' namely 'b', have a oneToOne relation with this instance.

Another instance of another entity, class 'C' namely 'c', have a oneToOne relation with this instance.

'a', 'b' and 'c' are stored in user http session after some actions.
At one time, I need to walk the object graph of 'c'. Since c's relationship with 'a' is lazy and unitialized at that time, I simply reattach 'a' (back from http session, remember) to current session (lock(a, LockMode.NONE)); then I reattach 'c' too, believing he will fetch 'a' from session cache and make the link.

But Instead, it throw an exception.

Isn't he supposed to use the cached instance (just reattached) and no burst into error like that ?

And yes, 'A' implements equals() and hashcode() (Using database identifier equality).

Any hint ???? :)

Thanks in advance,

sne.


Top
 Profile  
 
 Post subject: help !
PostPosted: Tue Oct 03, 2006 6:20 am 
Beginner
Beginner

Joined: Fri Sep 16, 2005 7:51 am
Posts: 25
* beg for help *

No one, really ???

I tried a lot of unsatisfying tweak and hack... any clue ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 03, 2006 8:22 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
the object a is already in there - use merge if you want to merge the state of the two objects.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 03, 2006 9:32 am 
Newbie

Joined: Thu Sep 21, 2006 12:30 pm
Posts: 2
Is it possible that when you re-attach object A, it is loading a copy of C from the database? Try debugging to see if this is the case.

_________________
Brian Besterman
Walker Digital Gaming, LLC


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 04, 2006 4:06 am 
Beginner
Beginner

Joined: Fri Sep 16, 2005 7:51 am
Posts: 25
brazbro : No, there is no reference from A to C (unidirectionnal from C to A only)

MAX : I'm not really confortable with the use of merge...
Its' this method you are refering to?
http://www.hibernate.org/hib_docs/v3/ap ... ang.Object)

I have read and explored "Hibernate in Action" and checked the HBN reference guide and nothing pointed me toward this solution. Therefore, the method description doesn't really satisfy me.

Is my use case so exceptionnal that it isn't explored in any documentation ? Isn't Hibernate supposed to perform and equality check between a reattached (then 'persistant') entity, and the same entity when it is referenced (even lazily) by another one ?

Max, can you develop a little bit the matter ???

Thank you in advance for your time ! :)

sne.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 04, 2006 4:12 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
merge is covered pretty well in hibernate in action as far as i remember.

Try and reread those sections again ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 04, 2006 4:26 am 
Beginner
Beginner

Joined: Fri Sep 16, 2005 7:51 am
Posts: 25
Yep, there is in fact a section
"5.2 Working with application transactions" that deals with 'merge'.

The subject of merging MODIFIED instances is described.

But in my case, instance 'a' si NOT modified.
It's simply what we could call a 'design issue' in the object model. The object graph has been exploded, and some references reversed, thus implying that B points to A, C points to A, and A know nothing of B and C.

To put it in a nutshell : must I consider my use case a simple 'particular case' of what is describer in chapter 5.2 in HIA ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 04, 2006 4:29 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
your object model *is* modified seen from the perspective of the db and hibernate.

In particular you don't have a consistent object graph (the associations does not represent what should actually be there - at least if i read you "graph" correctly)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 04, 2006 4:37 am 
Beginner
Beginner

Joined: Fri Sep 16, 2005 7:51 am
Posts: 25
Ok ok !

Have you some advices for me to improve my object graph ? A 'best practice' to refactor my case ?

In the origin, A was pointing to B and C.
Then we decided to remove those links for better A class reusability and 'reversed' the link... thus leading to what I exposed. With the know side effects...

No simple solution that doesn't implies this kind of thing :
http://www.objectmentor.com/resources/articles/dip.pdf
?

Again, thanks for your time ! ;)


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.