Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Vinnie Jenks
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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/projects/hibernate/browse/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):



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.

I'm using Hibernate 3.1.2 w/ MSSQL 2000.
 
Vinnie Jenks
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

thanks
 
Sebastian Hennebrueder
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
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
 
payal abcdf
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did anybody get a solution to this error?
 
Albert Gorski
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 65337
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.

Thanks!
bear
JavaRanch Sheriff
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic