-->
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.  [ 5 posts ] 
Author Message
 Post subject: Problem re-attaching existing objects to a new session
PostPosted: Thu Feb 16, 2006 2:45 pm 
Regular
Regular

Joined: Mon Nov 14, 2005 7:33 pm
Posts: 73
I asked this question a few months ago and never got an answer, hopefully someone can help me out this time. I think the problem I'm having is associated w/ a bug in Hibernate that's been around for a while:

http://opensource2.atlassian.com/projec ... se/HHH-511

I've got a setup where my DAO opens a session & transaction and a servlet filter commits and closes (per the instruction of Hibernate In Action - designing layered applications.)

In doGet() of my servlet I'm fetching an object, populating a form w/ the values (so they can be updated) and setting the object to an HttpSession.

In doPost() I get the object from the session, update the values from the form, and call saveOrUpdate() to persist it.

I call saveOrUpdate because they may be inserting a new record, not just updating an existing one.

This setup means that a session is opened/closed on doGet and another is opened/closed on doPost...so the problem I'm having is re-attaching an existing record on the session being utilized in doPost. Since the object I'm working with has two child objects (which are not mapped in the parent hbm.xml file) - apparently the save or saveOrUpdate methods are seeing the old object, not the new...as outlined in the error report link above.

Occasionally, I get this exception (same as bug report link above):

Code:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.app.Directory#1]
   at org.hibernate.engine.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:629)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:258)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:216)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
   at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:531)
   at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:523)
   at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:519)
...


Sometimes it works and the data is inserted/updated and sometimes it's not...and I get the exception...it's not consistent at all.

I've got several other forms where I use this same setup and it works flawlessly - but that is when working with single objects w/o children of their own.

Any ideas? I would have posted some code but it's long and wrapped up within many layers of my application...and probably wouldn't explain anything that I haven't already.

Hibernate version: 3.1.2

Name and version of the database you are using: MSSQL 2000


Top
 Profile  
 
 Post subject: session.merge()
PostPosted: Thu Feb 16, 2006 4:05 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
/**
* Copy the state of the given object onto the persistent object with the same
* identifier. If there is no persistent instance currently associated with
* the session, it will be loaded. Return the persistent instance. If the
* given instance is unsaved, save a copy of and return it as a newly persistent
* instance. The given instance does not become associated with the session.
* This operation cascades to associated instances if the association is mapped
* with <tt>cascade="merge"</tt>.<br>
* <br>
* The semantics of this method are defined by JSR-220.
*
* @param object a detached instance with state to be copied
* @return an updated persistent instance
*/
public Object merge(Object object) throws HibernateException;

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject: perfect!
PostPosted: Thu Feb 16, 2006 4:38 pm 
Regular
Regular

Joined: Mon Nov 14, 2005 7:33 pm
Posts: 73
Works beautifully, thanks for the help.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 19, 2006 6:51 am 
Newbie

Joined: Tue Mar 07, 2006 9:15 am
Posts: 3
What can I do with the same problem but Hibernate Version 2.1.7?

X


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 19, 2006 4:30 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Clear the object from the session then saveOrUpdate().


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