-->
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.  [ 15 posts ] 
Author Message
 Post subject: entity was not detached on Merge
PostPosted: Fri Dec 09, 2005 8:35 am 
Newbie

Joined: Mon Dec 05, 2005 10:25 am
Posts: 16
I'm trying merge object, which was loaded by query.
(When object is loaded by 'get' or 'load' no exception is thrown)


Hibernate version: 3.0.5

Mapping documents:

Code between sessionFactory.openSession() and session.close():
I'm using Spring HibernateDaoSupport

Code:
      Bonus b = (Bonus) getHibernateTemplate().execute(new HibernateCallback() {
         public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query q=session.createQuery("From Bonus b where b.employee=1 AND b.year=2004");
            Bonus b = (Bonus) q.uniqueResult();
            return b;
         }
      });
      getHibernateTemplate().merge(b);



Full stack trace of any exception that occurs:
org.hibernate.AssertionFailure: entity was not detached
org.hibernate.event.def.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:201)
org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:102)
org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:54)
org.hibernate.impl.SessionImpl.merge(SessionImpl.java:535)
org.hibernate.impl.SessionImpl.merge(SessionImpl.java:539)
org.springframework.orm.hibernate3.HibernateTemplate$23.doInHibernate(HibernateTemplate.java:665)
org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:311)
org.springframework.orm.hibernate3.HibernateTemplate.merge(HibernateTemplate.java:662)


Name and version of the database you are using: Oracle

The generated SQL (show_sql=true):
Hibernate: /* From Bonus b where b.employee=1 AND b.year=2004 */ select bonus0_.rok as rok, bonus0_.employee_id as employee2_, bonus0_.version_bonus as version3_3_, bonus0_.hodnota as hodnota3_ from MBO_BONUS bonus0_ where bonus0_.employee_id=1 and bonus0_.rok=2004
Hibernate: /* load ......model.Employee */ select employee0_.employee_id as employee1_0_, employee0_.version_employee as .... where employee0_.employee_id=?
Hibernate: /* load ....model.Bonus */ select bonus0_.rok as rok0_, bonus0_.employee_id as employee2_0_, bonus0_.version_bonus as version3_3_0_, bonus0_.hodnota as hodnota3_0_ from MBO_BONUS bonus0_ where bonus0_.rok=? and bonus0_.employee_id=?
ERROR [org.hibernate.AssertionFailure] - an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
org.hibernate.AssertionFailure: entity was not detached


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 09, 2005 11:13 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
merge expects the object you pass to it to be either associated with that session, or not associated to any session . It appears here that you are querying for the object in say Session1 then trying to immediatly merge that object with Session2.

getHibernateTemplate() opens a new session for method calls.


That's what these lines in the stack trace try to relate :)
Code:
org.hibernate.event.def.DefaultMergeEventListener.entityIsDetached

Code:
org.hibernate.AssertionFailure: entity was not detached



note, merging with another session will prove to be more difficult than merging with the original session as reassocating objects with sessions can be tricky sometimes

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 09, 2005 1:02 pm 
Newbie

Joined: Mon Sep 26, 2005 2:40 pm
Posts: 17
Location: Seattle
I'm not familiar with the getHibernateTemplate method you reference here, but I assume the merge(..) method on it is the same as the merge(..) method on the Session object.

My understanding differs from the previous poster, pretty much 180 degrees.

I believe is that merge exists specifically to merge objects created by another session. It doesn't actually merge that object, however, it returns an object for you that *is* merged. Thus, you need to change:

getHibernateTemplate().merge(b);

to:

b = getHibernateTemplate().merge(b);

Hope this helps,

Kevin


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 09, 2005 2:15 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
we don't disagree. I was trying to lead him towards the solution by telling him why he was getting that error.

Quote:
merge expects the object you pass to it to be either associated with that session, or not associated to any session .


What needs to be done is that object needs to be detached from the session it was loaded from first, before being attached(merge attaches it) to a new session.

If you are unaware of transient/persistant states of objects, it is a good read to help understand hibernate

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 09, 2005 2:37 pm 
Newbie

Joined: Mon Sep 26, 2005 2:40 pm
Posts: 17
Location: Seattle
I still think we disagree. Specifically, I don't think that merge(..) cares if the provided instance is transient, associated to another Session, or associated with the current Session.

Here's my understanding:

I'm using merge right now (3.1.rc3), and it happily takes objects associated with other sessions. In fact, that's the entire reason I'm using it.

Some methods do take either a transient object or one that is associated with the current session (delete, maybe update?), but merge isn't one of them.

The reason it doesn't care is that the merge method does not associate the provided reference with the session, it returns a *new* instance. The problem the OP is having is that he is ignoring that returned value.

Are you perhaps thinking of the lock(..) method? It *does* change the nature of the provided instance, and though the documentation I'm reading doesn't say so, I could see where it would complain if the provided instance wasn't either transient or already associated.

-Kevin


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 12, 2005 3:40 am 
Newbie

Joined: Mon Dec 05, 2005 10:25 am
Posts: 16
Zipwow wrote:
b = getHibernateTemplate().merge(b);


The same exception is throwed.
if getHibernaTemplate().merge(b) throw exception then b=getHibernateTemplate(b) throw exception too.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 2:21 pm 
Newbie

Joined: Mon Sep 26, 2005 2:40 pm
Posts: 17
Location: Seattle
Ahh, I misunderstood, I thought you were getting the exception when you attempted to save b, not when you tried to merge it.

I don't get this behavior from merge in 3.1. For 3.0, it sounds like kotchp's advice is best: either use the same session for both operations, or detatch it before you merge.

Personally, I hate the HibernateTemplate, and would take this approach:

Session session = getSession();
Bonus b=(Bonus) session.createQuery("From Bonus b where b.employee=1 AND b.year=2004").uniqueResult();

But there may be more "magic" to the HibernateTemplate that I'm missing.

-Zipwow


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 14, 2005 4:56 am 
Newbie

Joined: Mon Dec 05, 2005 10:25 am
Posts: 16
Zipwow wrote:

I don't get this behavior from merge in 3.1. For 3.0, it sounds like kotchp's advice is best: either use the same session for both operations, or detatch it before you merge.

-Zipwow


Could you write me how can I detach the object?

I cannot use the same session - I read the Object on the other place than I merge, and I don't know which session I get from getHibernateTemplate() :-/


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 14, 2005 4:57 am 
Newbie

Joined: Mon Dec 05, 2005 10:25 am
Posts: 16
Now I make marge by hand (load, compare version and save or throw Exception)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 15, 2005 5:01 am 
Newbie

Joined: Mon Dec 05, 2005 10:25 am
Posts: 16
Now I find new fact. The problem is with POJO which has NOT automaticaly generated ID (primary key) in hbm.xml file is not tag <generator> in tag <id> (becouse it is absurd in the context)[/b]


Top
 Profile  
 
 Post subject: so ....
PostPosted: Fri Feb 10, 2006 5:17 am 
Newbie

Joined: Mon Jul 12, 2004 2:52 am
Posts: 17
I got the same problem
My POJO is using assigned key and have few 1-N lazy-loaded relations

Could you post your solution?


Top
 Profile  
 
 Post subject: Re: so ....
PostPosted: Mon Feb 13, 2006 3:31 am 
Newbie

Joined: Mon Dec 05, 2005 10:25 am
Posts: 16
Kei wrote:
I got the same problem
My POJO is using assigned key and have few 1-N lazy-loaded relations

Could you post your solution?


Problem is that the POJO has not generator for primary key.
So you cannot use merge.

My solution was> I used saveOrUpdate and check for version I make by hand. Load "old" pojo from DB and compare property version.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 13, 2006 3:55 am 
Newbie

Joined: Mon Jul 12, 2004 2:52 am
Posts: 17
I've just discovered that my problem is not due to the update to the main entity, but the N-relations of the main entity.

If I load the entity and its relations (cascade=all-delete-orphan) in a previous session, then save it in another session, exception would raise no matter i use saveOrUpdate or merge...

i posted my problem in : http://forum.hibernate.org/viewtopic.php?t=951107

any clue?


Top
 Profile  
 
 Post subject: oops
PostPosted: Mon Feb 13, 2006 3:56 am 
Newbie

Joined: Mon Jul 12, 2004 2:52 am
Posts: 17
sorry... wrong link
should be this one: http://forum.hibernate.org/viewtopic.php?t=955422


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 13, 2006 3:58 am 
Newbie

Joined: Mon Dec 05, 2005 10:25 am
Posts: 16
Kei wrote:
....

i posted my problem in : http://forum.hibernate.org/viewtopic.php?t=951107

any clue?


It's my problem :-). (951107)
May be something bad is in mapping ...


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