This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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 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.
Joined: Apr 26, 2004
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!
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?
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.
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="http://www.laliluna.de" target="_blank" rel="nofollow">www.laliluna.de</a>
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.