-->
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.  [ 11 posts ] 
Author Message
 Post subject: again: NonUniqueObjectException
PostPosted: Tue Jun 29, 2004 4:58 am 
Beginner
Beginner

Joined: Thu Oct 30, 2003 6:29 am
Posts: 30
Location: Germany, KA
hi,
don't want to annoy but this exception IMHO unnecessary and stupid.

sorry, I can't provide any mapping- or code-snippets because I can't set up a test-case that reproduces this exception, although I keep getting it from time to time in application (those occurences ARE reproducable, but I worked hard on extracting the same enviroment to a test case without success)

in my oppinion a NonUniqueObjectException is a complete waste of time, since I expect HIBERNATE to deal with such occurences. I am not willing to understand the incapability of hibernate managing instances of entities transparently. Of course I do have a entity in my objectmodel that I might want to reference again:

i.e.: I have a HashSet of Groups already bound to my objectmodel. I retrieve a special list of Groups from db using s.th. like
Query q = sess.getHibernateSession().createQuery("from c in class org.xinity.base.adco.XinityBaseGroup where c.personal=? order by c.name");
q.setBoolean(0,personal);
return q.list();

I iterate over the returned list selecting a subset of entities according to their attributes and add this subset to my objectmodel.

boom, NonUniqueObjectException.

I think hibernate should return only a singleton.instance of an entity. that would be really convinient.

What is you oppinion, am I missing s.th.?
thanks
marie


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 29, 2004 8:28 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You basically said to Hibernate, load this class having id = 1
If Hibernate get 2 or more rows for this particular id, it complains, since it cannot know the one that is good for you.

Your DB does not respect the unicity. This exception is valid.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: again: NonUniqueObjectException
PostPosted: Wed Jun 30, 2004 8:50 am 
Beginner
Beginner

Joined: Thu Oct 30, 2003 6:29 am
Posts: 30
Location: Germany, KA
mhh, no, actually I said to hibernate: please save my objectmodel and hibernate tells me, that I have two objects having the same id, but not the same VM-Identity within my model and that this is not allowed.

And I am wondering, WHY I have two instances of a the same entity? I want hibernate to provide a singel instance of this entity.

objA = (XinityBaseObject) this.session.getHibernateSession().get(A_CLASS, uid);

--> returns an entity A(1) { B(2), C(3) }

objB = (XinityBaseObject) this.session.getHibernateSession().get(B_CLASS, uidB);

--> returns an entity B(2) { A(4!!), C(3) }
^ why VM-Identity 4???
objA.getBs().add(objB);
this.session.getHibernateSession().save(A);

--> NonUniqueObjectException


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 30, 2004 8:58 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
are your sure of your unicity and data integrity?

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject: Re: again: NonUniqueObjectException
PostPosted: Tue Jul 06, 2004 11:16 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Marie wrote:
--> returns an entity A(1) { B(2), C(3) }

objB = (XinityBaseObject) this.session.getHibernateSession().get(B_CLASS, uidB);

--> returns an entity B(2) { A(4!!), C(3) }
^ why VM-Identity 4???


In the same session, an object cannot be loaded twice, I would say you're wrong. Try to use you debugger to find some more tips, it's hard to help you on a forum :-(

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Testcase for NonUniqueObjectException
PostPosted: Wed Jul 28, 2004 6:43 am 
Beginner
Beginner

Joined: Thu Oct 30, 2003 6:29 am
Posts: 30
Location: Germany, KA
hi,
back from holiday, back to my beloved NonUniqueObjectException.
I extracted a test-case that illustrates my problem. many thanks to those making the effort to look at it. your help is much appreciated. you'll find it at http://inmeco.de/snippets/NonUniqueObje ... stCase.zip

in my oppinion, this is a normal use-case:

I have a User that is looged in, thus held over multiple hibernate-sessions. This user is added to Issue (1:m User:Issue) and a Issue is related to a Version (1:m Version:Issue). Works fine, but when adding a new Issue to a Version freshly loaded from DB, hibernate says, my user - associated to the new Issue - is not unique. Of course it is not, because it is loaded with the Version from DB AND is still held by my app, of course I could reload the user using the id, but I do have many of those session-spanning entities, because each user-interaction opens a seperate hibernate-session and a entity may be persisted after many such interactions. I think I see my mistake, but I can figure out how to approach this in a better way. the same thing with loading entities. I have a entity loaded from db that is related to another one. when I laod the other entity I have the same problem... two instances of a single entity. I can't know each entity that is referenced by some other! how about special criteria when querying for an entity? how am I expected to load it? dive into my model of many entities and look for one with the same id? that is not cool!
I basically see two options:

[i] I could implement long-run hibernate-sessions and trash my 'TransientObjects'. But this is discouraged by documentation.

[ii] I could catch that exception and reload the entity from database, fill it with data from the 'broken' transient-object and do so for each of its related entities.

[iii] ejb :)?

[iv] request a hibernate-feature that allows to 'park' entities that are modified in multiple session, but get persisted only if the user presses 'save'. a kind of long-running super-session?

[v] request a hibernate-feature that allows hooking entity-retrieval, so my TransientObject-Manager is asked by hibernate for an entity with a certain uid?

thank you
marie


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 28, 2004 6:46 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Quote:
I have a User that is looged in, thus held over multiple hibernate-sessions.


Not neccessarily a good idea. Have you considered reading Hibernate in Action? It has a very detailed discussion of long-running application transactions. I doubt someone has the time to debug your source...

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 28, 2004 7:39 am 
Newbie

Joined: Tue Jul 27, 2004 6:35 am
Posts: 2
It would be nice if hibernate had a config setting to use equals() on an object instead of using == for a pointer reference or just used it by default. maybe it is a performance tweak but man this grates because you are forced to always check your model.

I resolved this by writing a simple utility, see this post
http://forum.hibernate.org/viewtopic.php?t=932855


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 28, 2004 8:03 am 
Beginner
Beginner

Joined: Thu Oct 30, 2003 6:29 am
Posts: 30
Location: Germany, KA
christian wrote:
Have you considered reading Hibernate in Action?

it is not published, yet, is it?

christian wrote:
I doubt someone has the time to debug your source...

yes, i am afraid, i believe so too. although it is very little code and (despite the hibernate.properties) ready to run. it jsut illustrates the problem and shows more my buggy approach than a typical code-bug.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 28, 2004 8:04 am 
Beginner
Beginner

Joined: Thu Oct 30, 2003 6:29 am
Posts: 30
Location: Germany, KA
rizo wrote:
I resolved this by writing a simple utility, see this post
http://forum.hibernate.org/viewtopic.php?t=932855


nice! thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 28, 2004 9:15 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You can already get HiA through MEAP, theres a banner on the left side on hibernate.org. It is in print right now, ready in the next weeks.

About this utility class: The whole equals()/== (or better, identity vs. equality) is much more complex that many people here think. It's actually not really Hibernate-related, but it appears when you use Hibernate and for many developers, this is the first time they have to think about it. There is no simple, one-line explanation. Read the book :)

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


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