Hello,
I'm experiencing problems using Hibernate in a WebContainer. I'm using Session per Request implementing the Session as ThreadLocal Variable within the Request.
Following the MVC paradigma I have Model classes that are persistable. These Model classes are hold within the HttpSession (Shopping card functionality) unless the user proceedes the workflow and commits the transaction.
The problem now is, that I'm adding models to a collection within different Hibernate Sessions. Therefore I open a new Session so that the Model from different Hibernate Sessions get reattached and do not belong to specific Hibernate Sessions that are no longer available.
To be more specific I have a Produkt that has a Set of Packung. The Packung itself has a many-to-one relationship to Mengenart, so one Packung holds exactly one Mengenart. Hibernate tells me that Mengenart was alreday assigned to another session. The interesting point is, that the error only occurs if I use exactly the same id for both Mengenart within the Packung set. If the id's are different everything works fine and is persisted.
My Question now is, wheter I need to use Long running sessions that are kept within the HttpSession or wheter I do something wrong with the Reattachment of Persistent Objects? I thought it would be possible to open a new Session and perform an update of the Objects?
Hibernate version: 2.1.4
Mapping documents: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="at.sozvers.eweko.model.Packung" table="MEPP" dynamic-update="false" dynamic-insert="false" >
<id name="id" column="id" type="java.lang.Long" unsaved-value="null"> <generator class="net.sf.hibernate.id.SequenceGenerator"> <param name="sequence">hibernate_sequence</param> </generator> </id> <version name="version" type="java.lang.Long" column="VERSION" access="property" unsaved-value="undefined"/> <many-to-one name="mengenart" class="at.sozvers.eweko.model.Mengenart" cascade="save-update" outer-join="auto" update="true" insert="true" access="property" column="MART" /> <many-to-one name="produkt" class="at.sozvers.eweko.model.Produkt" cascade="none" outer-join="auto" update="true" insert="true" access="property" column="MEPR" /> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="at.sozvers.eweko.model.Mengenart" table="MEMART" dynamic-update="false" dynamic-insert="false">
<id name="id" column="id" type="java.lang.Long" unsaved-value="null"> <generator class="net.sf.hibernate.id.SequenceGenerator"> <param name="sequence">hibernate_sequence</param> </generator> </id> <version name="version" type="java.lang.Long" column="VERSION" access="property" unsaved-value="undefined"/> <property name="code" type="java.lang.String" update="true" insert="true" access="property" column="MART" length="3" not-null="true"/> <property name="langbeschreibung" type="java.lang.String" update="true" insert="true" access="property" column="BLMART" length="60"/> </class> </hibernate-mapping>
Code between sessionFactory.openSession() and session.close(): Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); session.update(model); tx.commit(); session.flush();
Full stack trace of any exception that occurs: 07:11:20,031 ERROR [STDERR] net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 6, of class: at.sozvers.eweko.model.Mengenart at net.sf.hibernate.impl.SessionImpl.checkUniqueness(SessionImpl.java:1673) at net.sf.hibernate.impl.SessionImpl.doUpdateMutable(SessionImpl.java:1442) at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1469) at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1392) at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114) at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436) at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503) at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:482) at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1474) at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1392) at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114) at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436) at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:526) at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452) at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503) at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:482) at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1474) at net.sf.hibernate.impl.SessionImpl.update(SessionImpl.java:1354)
Name and version of the database you are using: Oracle 9i
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
|