Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Object Relational Mapping and the fly likes Hibernate: Problem re-attaching existing objects to a new session Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate: Problem re-attaching existing objects to a new session" Watch "Hibernate: Problem re-attaching existing objects to a new session" New topic

Hibernate: Problem re-attaching existing objects to a new session

Vinnie Jenks
Ranch Hand

Joined: Apr 26, 2004
Posts: 207
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:

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 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 outlined in the error report link above.

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

Sometimes it works and the data is inserted/updated and sometimes it's not...and I get the'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.

I'm using Hibernate 3.1.2 w/ MSSQL 2000.
Vinnie Jenks
Ranch Hand

Joined: Apr 26, 2004
Posts: 207
Someone figured it out for me on the Hibernate forums - simply call Session.merge() instead of saveOrUpdate and it forces the object to use the correct session state - just in case anyone comes across this problem!
nikhil k

Joined: May 17, 2006
Posts: 1
I got the same problem and moved an inch forward with session.merge(), but i get a nasty org.hibernate.TransientObjectException saying that the child records couldn't be saved. Did you get any luck further with your problem?

Sebastian Hennebrueder
Ranch Hand

Joined: Sep 09, 2004
Posts: 49
I am not a great fan of OpenSessionInView pattern.

I prefer to open close sessions in my business logic.

For your session.merge problem you will have to save the children you added to your entry either explicitly or you must specify cascade="all" in your parent.

Regards Sebastian

Things get always more complicated as expected.<br />Author of eBook Hibernate 3 Developer Guide by example<br />Tutorials about Hibernate, EJB, Struts, JSF <a href="" target="_blank" rel="nofollow"></a>
payal abcdf

Joined: Jul 26, 2006
Posts: 1
Did anybody get a solution to this error?
Albert Gorski

Joined: Sep 17, 2003
Posts: 2
the problem could be also solved by removing cascade from many-to-one mapping.
I have had problem with identifier when I have had a cascade="all" in the mapping file of the child table.

hopefully helps ...
jason none

Joined: Apr 24, 2006
Posts: 2
i had this same issue with attempting to call list() on a Criteria object of a session. My solution was to call sessionFactory.openSession for creating Criteria objects and use the current session to persist/merge.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63858

"jason none",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

JavaRanch Sheriff

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: Hibernate: Problem re-attaching existing objects to a new session
jQuery in Action, 3rd edition