I am getting the following error:
net.sf.hibernate.HibernateException: Another object was associated with this id
(the object with the given id was already loaded): [com.xxx.uom.component.Cont
actMechanismType#1]
at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1393)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1329)
at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:113)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:339)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:406)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:385)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:774)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:678)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1324)
at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:113)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:339)
at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:429)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:355)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:406)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:385)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:824)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:678)
at MultipleSession.<init>(MultipleSession.java:83)
at MultipleSession.main(MultipleSession.java:98)
My hbms:
Person1.hbm.xml is
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping default-cascade="save-update">
<class name="com.xxx.uom.component.Person1" table="PERSON1" dynamic-update="true">
<!--
A 32 hex character is our surrogate key.
It's automatically generated by Hibernate
with the UUID pattern.
-->
<id name="id" type="string">
<column name="ID" sql-type="varchar2(32)" not-null="true" length="32"/>
<generator class="uuid.hex"/>
</id>
<version name="version" type="long" column="VERSION"/>
<property name="lastName" type="string">
<column name="CURRENT_LAST_NAME" sql-type="varchar2(40)" not-null="false"/>
</property>
<property name="firstName" type="string">
<column name="CURRENT_FIRST_NAME" sql-type="varchar2(40)" not-null="false"/>
</property>
<property name="middleName" type="string">
<column name="CURRENT_MIDDLE_NAME" sql-type="varchar2(40)" not-null="false"/>
</property>
<property name="personalTitle" type="string">
<column name="CURRENT_PERSONAL_TITLE" sql-type="varchar2(40)" not-null="false"/>
</property>
<property name="suffix" type="string">
<column name="CURRENT_SUFFIX" sql-type="varchar2(40)" not-null="false"/>
</property>
<property name="nickName" type="string">
<column name="CURRENT_NICKNAME" sql-type="varchar2(40)" not-null="false"/>
</property>
<property name="gender" type="string">
<column name="GENDER" sql-type="varchar2(1)" not-null="false"/>
</property>
<property name="birthDate" type="java.util.Date">
<column name="BIRTH_DATE" sql-type="date" not-null="false"/>
</property>
<property name="height" type="java.lang.Long">
<column name="HEIGHT" sql-type="number(10)" not-null="false"/>
</property>
<property name="weight" type="java.lang.Long">
<column name="WEIGHT" sql-type="number(10)" not-null="false"/>
</property>
<property name="maidenName" type="string">
<column name="MOTHERS_MAIDEN_NAME" sql-type="varchar2(40)" not-null="false"/>
</property>
<property name="maritalStatus" type="string">
<column name="MARITAL_STATUS" sql-type="varchar2(1)" not-null="false"/>
</property>
<property name="ssn" type="java.lang.Long">
<column name="SOCIAL_SECURITY_NUMBER" sql-type="number(10)" not-null="false"/>
</property>
<property name="ppno" type="java.lang.Long">
<column name="CURRENT_PASSPORT_NUMBER" sql-type="number(10)" not-null="false"/>
</property>
<property name="expiryDate" type="java.util.Date">
<column name="CURRENT_PASSPORT_EXPIRE_DATE" sql-type="date" not-null="false"/>
</property>
<property name="experience" type="java.lang.Long">
<column name="TOTAL_YEARS_WORK_EXPERIENCE" sql-type="number(10)" not-null="false"/>
</property>
<property name="comments" type="string">
<column name="COMMENTS" sql-type="varchar2(100)" not-null="false"/>
</property>
<set name="contactMechanism" cascade="all" inverse="true" lazy="false">
<key column="PERSON_ID"/>
<one-to-many class="com.xxx.uom.component.ContactMechanism"/>
</set>
</class>
</hibernate-mapping>
ContactMechanismType.hbm.xml is
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping default-cascade="save-update">
<class name="com.xxx.uom.component.ContactMechanismType" table="CONTACT_MECHANISM_TYPE" dynamic-update="true">
<!--
A 32 hex character is our surrogate key.
It's automatically generated by Hibernate
with the UUID pattern.
-->
<id name="id" type="string" unsaved-value="null" >
<column name="ID" sql-type="varchar2(32)" not-null="true" length="32"/>
<generator class="uuid.hex"/>
</id>
<version name="version" type="long" column="VERSION"></version>
<!-- A Contact Mechanism Type has to have a description, but it shouldn' be too long. -->
<property name="description" type="string">
<column name="DESCRIPTION" sql-type="varchar2(100)" not-null="true"/>
</property>
<set name="contactMechanism" cascade="all" inverse="true" lazy="true">
<key column="CM_TYPE_ID"/>
<one-to-many class="com.xxx.uom.component.ContactMechanism"/>
</set>
</class>
</hibernate-mapping>
ContactMechanism.hbm.xml is
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping default-cascade="save-update">
<class name="com.xxx.uom.component.ContactMechanism" table="CONTACT_MECHANISM" dynamic-update="true">
<!--
A 32 hex character is our surrogate key.
It's automatically generated by Hibernate
with the UUID pattern.
-->
<id name="id" type="string" unsaved-value="null" >
<column name="ID" sql-type="varchar2(32)" not-null="true" length="32"/>
<generator class="uuid.hex"/>
</id>
<version name="version" type="long" column="VERSION"/>
<!-- Many Contact Mechanisms has one CM Type -->
<many-to-one name="contactMechanismType"
class="com.xxx.uom.component.ContactMechanismType"
column="CM_TYPE_ID"
not-null="true"/>
<!-- Many Contact Mechanisms has one Person -->
<many-to-one name="person1"
class="com.xxx.uom.component.Person1"
column="PERSON_ID"
not-null="true"/>
<joined-subclass name="com.xxx.uom.component.PostalAddress" table="POSTAL_ADDRESS">
<key column="ID"/>
<property name="address1" type="string">
<column name="ADDRESS1" sql-type="varchar2(100)" not-null="true"/>
</property>
<property name="address2" type="string">
<column name="ADDRESS2" sql-type="varchar2(100)" not-null="false"/>
</property>
<property name="city" type="string">
<column name="CITY" sql-type="varchar2(20)" not-null="true"/>
</property>
<property name="state" type="string">
<column name="STATE" sql-type="varchar2(30)" not-null="true"/>
</property>
<property name="zipCode" type="string">
<column name="ZIPCODE" sql-type="varchar2(20)" not-null="true"/>
</property>
</joined-subclass>
<joined-subclass name="com.xxx.uom.component.TelecommunicationNumber" table="TELECOMMUNICATION_NUMBER">
<key column="ID"/>
<property name="phoneNumber" type="string">
<column name="PHONE_NUMBER" sql-type="varchar2(20)" not-null="true"/>
</property>
<property name="faxNumber" type="string">
<column name="FAX_NUMBER" sql-type="varchar2(20)" not-null="false"/>
</property>
</joined-subclass>
<joined-subclass name="com.xxx.uom.component.ElectronicAddress" table="ELECTRONIC_ADDRESS">
<key column="ID"/>
<property name="emailId" type="string">
<column name="EMAIL_ID" sql-type="varchar2(30)" not-null="true"/>
</property>
<property name="webUrl" type="string">
<column name="WEB_URL" sql-type="varchar2(30)" not-null="false"/>
</property>
</joined-subclass>
</class>
</hibernate-mapping>
I am trying to add a new postal address for an existing person and also associating it as "POSTAL ADDRESS" contact mechanism type. My contact_mechanism_type table is already loaded with required types.
Below is my code:
public class MultipleSession {
MultipleSession() {
Session s1 = null;
Session s2 = null;
Session s3 = null;
try {
Set children = null;
ClassLoader classLoader = this.getClass().getClassLoader();
URL configURL = classLoader.getResource("hibernate-jdbc.cfg.xml");
SessionFactory sf =
new Configuration().configure(configURL).buildSessionFactory();
Transaction tx1 = null;
Transaction tx2 = null;
Transaction tx3 = null;
ContactMechanismType cmt = null;
ContactMechanism cm = null;
PostalAddress pa = null;
Query q = null;
List list = null;
int n = 0;
// Open session -- 1.
System.out.println("Start Sesion 1 -------->>> ");
s1 = sf.openSession();
String id = "2c9785eafa9b66fc00fa9b6a5d940003";
Person1 person = (Person1)s1.load(Person1.class, id);
System.out.println("FirstName -------->>> " + person.getFirstName());
System.out.println("LastName -------->>> " + person.getLastName());
System.out.println("ContactMechanism().size -------->>> " + person.getContactMechanism().size());
children = person.getContactMechanism();
s1.clear();
s1.flush();
try {s1.close();} catch(Exception e){e.printStackTrace();};
System.out.println("End Sesion 1 -------->>> ");
// Open session -- 2.
System.out.println("Start Sesion 2 -------->>> ");
s2 = sf.openSession();
q = s2.createQuery(" from ContactMechanismType cmt where description='POSTAL ADDRESS'");
list = q.list();
cmt = (ContactMechanismType)list.get(0);
System.out.println("id -------->>> " + cmt.getId());
System.out.println("Description -------->>> " + cmt.getDescription());
s2.flush();
try {s2.close();} catch(Exception e){e.printStackTrace();};
System.out.println("End Sesion 2 -------->>> ");
// Open session -- 3.
System.out.println("Start Sesion 3 -------->>> ");
s3 = sf.openSession();
tx3 = s3.beginTransaction();
s3.clear();
pa = new PostalAddress();
pa.setAddress1("Line1");
pa.setAddress2("Line2");
pa.setCity("City");
pa.setState("ST");
pa.setZipCode("99999");
pa.setPerson1(person);
pa.setContactMechanismType(cmt);
children.add(pa);
person.setContactMechanism(children);
s3.save(person);
s3.flush();
tx3.commit();
try {s3.close();} catch(Exception e){e.printStackTrace();};
System.out.println("End Sesion 3 -------->>> ");
}
catch(Exception e){
e.printStackTrace();
}
finally {
}
}
I am using multiple sessions because I would be using this in my action class (servlet) and making calls to my session bean to load objects and save objects. So I cannot do all the loads and updates in one session.
Your help is highly appreciated.
|