-->
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.  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: why methond throws NonUniqueObjectException ?
PostPosted: Tue Dec 20, 2005 12:27 pm 
Newbie

Joined: Fri Dec 24, 2004 4:09 am
Posts: 11
Under some circumstance(not all the time) following methond throws NonUniqueObjectException

Code:
public FooObject getFooObject (Class clazz, long id, Session session) throws HibernateException{
   Criteria criteria = session.createCriteria(clazz);
   criteria.add(Expression.eq(“id”, new Long(id)));
   return (Executor) criteria.uniqueResult();
}


while the following method never throws NonUniqueObjectException
Code:
public FooObject getFooObject(Class clazz, long id,Session session) throws HibernateException{
   return (FooObject)session.get(clazz,new Long(id));
}


First question: what is the difference between querying using Criteria and loading using session.get()?

Second question: what should I do if I have to load object with composite identifier?

In following method I can not replace query by get.

Code:
public FooObject getFooObject (Class clazz, long id, Session session) throws HibernateException{
   Criteria criteria = session.createCriteria(clazz);
   criteria.add(Expression.eq(“id”, new Long(id)));
   criteria.add(Expression.eq(“type”, new Long(id)));
   return (Executor) criteria.uniqueResult();
}


NOTE: I'm using Hibernate 2.1.8

Is this problem appears in latest Hibernate version?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 21, 2005 2:05 pm 
Regular
Regular

Joined: Fri Dec 17, 2004 10:38 am
Posts: 54
There are number of post concerning this toping in the forum (just use Search to see the number) but there was no single worth-while advice...
Unfortunately it seems to be a taboo topic for Hibernate team (even 'Hibernate in Action' does not mention NonUniqueObjectException situations, just state session-level cache uniqueness).
Very sad....

I have similar situation with reasigning detached objects to a session.
Have to bend my logic to overcome this Hibernate restriction and use saveOrUpdateCopy instead of saveOrUpdate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 21, 2005 2:16 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Of course this also an FAQ: http://www.hibernate.org/?cmd=srchdoc&q ... tException


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 21, 2005 2:30 pm 
Regular
Regular

Joined: Fri Dec 17, 2004 10:38 am
Posts: 54
Thank you, Christian, for reply!
But the thing is -- I do not know beforehand, is my object already associated
The object can be associated by another object by association, or can be not
I just do not know.

as for saveOrUpdateCopy -- it just brake logic.
I pass object from higher layer to persistance one and reassociate it.
And use it in logic again...
I do not want to use another instance insted of passed one in logic layer.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 21, 2005 2:45 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
So do what the documentation says. Reattach before you do anything else. This is really a simple problem with simple solutions. The original poster has a different problem, but nobody can guess what it could be, because he ignored http://www.hibernate.org/ForumMailingli ... AskForHelp


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 21, 2005 3:03 pm 
Regular
Regular

Joined: Fri Dec 17, 2004 10:38 am
Posts: 54
I'd realy like to do it right! but consider situation.
Have two persistent classes: container and component.
with bidirectional association (lazy or not -- does not matter).

attached container first -- component association will fail if component is associated w container.
and vice versa.

ok, i can check container for component before component association... but if association is deep (container contains another container, contains another,...., contains component)? traverse all possible associations?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 21, 2005 3:26 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Last response: "the first thing is reattachment". Open a Session, reattach your detached graph, no exception.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 21, 2005 3:41 pm 
Regular
Regular

Joined: Fri Dec 17, 2004 10:38 am
Posts: 54
Christian, I'm deeply appologies, but I did not get you
1) Attach graph
2) Attach component (which is in graph)
3) no NonUniqueObjectException???
This is the way? And all this with Hibernate 2.1?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 21, 2005 4:57 pm 
Newbie

Joined: Fri Dec 24, 2004 4:09 am
Posts: 11
christian wrote:


are you sure you read the question to the end?


Top
 Profile  
 
 Post subject: hibernate 3.1
PostPosted: Thu Dec 22, 2005 5:09 am 
Newbie

Joined: Thu Dec 22, 2005 5:05 am
Posts: 2
fuzebest wrote:
And all this with Hibernate 2.1?

hmm. I just checked your idea with hibernate 3.1 -- it throws NonUniqueObjectException as well


Top
 Profile  
 
 Post subject: Re: hibernate 3.1
PostPosted: Fri Dec 23, 2005 5:22 am 
Newbie

Joined: Fri Dec 24, 2004 4:09 am
Posts: 11
doe wrote:
fuzebest wrote:
And all this with Hibernate 2.1?

hmm. I just checked your idea with hibernate 3.1 -- it throws NonUniqueObjectException as well


I can suggest try to AVOID Non composite relations via collections i.e. if you have Container and Item classes and Item can exist without Container or can present in more than ONE container.
DO NOT make collection of items in Container and DO NOT make collection of containers in Item class. Instead you'll have to create third class and hold realation information in it.

Relations via collections work well only if you have composite relation between classes.
i.e. Class Container contains collection of Item and item can be accesses only my via containers methods and can't exists without Container.

PS I can't understand hibernate developers position ignoring posting.
IMHO ignoring the posts won't solve any problem.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 26, 2005 8:15 am 
Newbie

Joined: Thu Dec 22, 2005 5:05 am
Posts: 2
Yes. I think you are right. It's hard to model aggregation relations between domain object.
Unfortunately this restriction imposes serious restrictions on persistent model, you have to develop "hibernate-specific" model.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 28, 2005 4:36 am 
Regular
Regular

Joined: Fri Dec 17, 2004 10:38 am
Posts: 54
Is this you personal opinion or these restrictions were described in hibernate documentation?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 25, 2006 9:35 am 
Regular
Regular

Joined: Wed May 11, 2005 11:57 pm
Posts: 80
christian wrote:


The FAQ points out one possible solution, but does not really talk about what causes the exception. Is there a code example that shows what might cause this to happen?

I'm getting this exception at the end of my transaction when I call transaction.commit(), no idea why.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 20, 2006 1:27 am 
Beginner
Beginner

Joined: Mon Oct 11, 2004 2:22 am
Posts: 41
when i get this issue and the cause is not obvious and therefore not tidy to fix, i do the following:

1. make sure you have the session's version of the object
- i have a utility method 'reAquire' which basically does the following:
Code:
object = getSession().load(object.getClass(), object);


2. make necissary changes to object

3. update() or saveOrUpdate() as necissary

I haven't found a case where this doesn't work yet.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 16 posts ]  Go to page 1, 2  Next

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.