I promise I searched the heck outta this forum, google, and all of cyberspace before coming here to ask you guys what the heck is going on. My next step after this forum post is to pull my hair right out of my head.
my version: 3.2.1.ga , but i have also tested in 3.3.2.GA, and the EXACT same result is happening there too, so I'm sure it's something I am doing wrong.
Alrighty, here is my goal:
1. User decides to cancel what they've done so far. They
click cancel2. I will open a
new session and then...
3. I will
refresh() the object they are working on so it is populated with whatever is in DB
4. I will
close session5. User will work on the object again, and
make changes to the data in some of the attributes
6. User will
click save7. I will open a
new session and then...
8. I will
update() the object so that their recent changes go into DB.
BANG RIGHT HERE I GET
NonUniqueObjectException.
9. Finally { close session }
Code:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [us.mn.state.dot.puma.model.RelationshipDTO#1243]
Obviously 1243 is the primary key of the object I'm trying to refresh and then update in this example, but it will happen with any primary key....
Some background:
This is a fully functioning application, the cancel feature is something I am just adding in now. The user can open up an existing object and make changes and save/update it all day long without any NonUniqueObjectException. It is only after a refresh that i cannot save/update!!
My model:
RelationshipDTO has many-to-one relationship to an ElementDTO object
Code:
<hibernate-mapping package="us.mn.state.dot.puma.model">
<class name="RelationshipDTO" table="BUNDLE_RELATIONSHIP" lazy="false">
<id name="relationshipId" column="BUNDLE_RELATIONSHIP_ID">
<generator class="sequence">
<param name="sequence">BUNDLE_RELATIONSHIP_ID</param>
</generator>
</id>
<property name="primeRelationship" column="PRIME_ELEMENT_FLAG" type="yes_no" />
<many-to-one name="bundle" class="BundleDTO" column="BUNDLE_ID" cascade="save-update,persist"/>
<many-to-one name="element" class="ElementDTO" column="ELEMENT_ID" cascade="all" />
<many-to-one name="primeElement" class="ElementDTO" column="PRIME_ELEMENT_ID" cascade="save-update,persist"/>
</class>
</hibernate-mapping>
and my ElementDTO:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="us.mn.state.dot.puma.model">
<class name="ElementDTO" table="ELEMENT" lazy="false">
<id name="elementId" type="java.lang.Long" column="ELEMENT_ID">
<generator class="assigned" />
</id>
<property name="spNumber" column="CORE_SP_NBR" type="string" />
<property name="spExt" column="SP_EXT" type="string" />
<property name="description" column="ELEMENT_DESC" />
<set name="relationships" lazy="false" table="BUNDLE_RELATIONSHIP"
cascade="all" inverse="true">
<key column="ELEMENT_ID"/>
<one-to-many class="RelationshipDTO" />
</set>
</class>
<sql-query name="element.nextId">select ELEMENT_ID_SEQ.nextval from dual</sql-query>
</hibernate-mapping>
FURTHERMORE:
I thought the problem would have to do with the fact that I "detached" the object after refreshing by closing the session. Nope! Just for testing purposes, I attempted to refresh and then update all within the same session - same problem. Here's my code:
Code:
session.refresh(dto);
Transaction tx = session.beginTransaction();
session.update(dto);
tx.commit();
session.close();
any help? losing my marbles here