Hi,
I am getting the following exception when using composite id
net.sf.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.abc.model.MyTable2 at net.sf.hibernate.impl.SessionImpl.throwTransientObjectException(SessionImpl.java:2652)
at net.sf.hibernate.impl.SessionImpl.getEntityIdentifierIfNotUnsaved(SessionImpl.java:2644)
at net.sf.hibernate.type.EntityType.getIdentifier(EntityType.java:66)
at net.sf.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:46)
at net.sf.hibernate.type.ComponentType.nullSafeSet(ComponentType.java:154)
at net.sf.hibernate.loader.Loader.bindPositionalParameters(Loader.java:673)
at net.sf.hibernate.loader.Loader.prepareQueryStatement(Loader.java:712)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:184)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:132)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:830)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:850)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:57)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:49)
at net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:420)
at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2044)
at net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:1918)
at net.sf.hibernate.impl.SessionImpl.load(SessionImpl.java:1847)
There are three tables with the first table MyTable1 having composite key. And the mapping files for all the tables are listed below.
Mapping for MyTable1 is :
Code:
<?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="com.abc.MyTable1" table="mytable1" dynamic-update="false" dynamic-insert="false">
<composite-id name="table1PK" class="com.abc.MyTable1PK">
<key-property name="table1Id" column="table1_id"/>
<key-many-to-one name="table2Id" class="com.abc.MyTable2" column="table2_id"/>
<key-many-to-one name="table3Id" class="com.abc.MyTable3" column="table3_id"/>
</composite-id>
<property name="tableOneColOne" type="java.lang.String" update="true" insert="true" column="column_1_1"/>
<property name="tableOneColTwo" type="java.lang.String" update="true" insert="true" column="column_1_2"/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-MyTable1.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
Mapping for MyTable2 is :
Code:
<?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="com.abc.MyTable2" table="mytable2" dynamic-update="false" dynamic-insert="false">
<id name="table2Id" column="table2_id" type="java.lang.String" unsaved-value="any">
<generator class="assigned"></generator>
</id>
<property name="tableTwoColOne" type="java.lang.String" update="true" insert="true" column="column_2_1"/>
<property name="tableTwoColTwo" type="java.lang.String" update="true" insert="true" column="column_2_2"/>
<set name="tableOneItems2">
<key column="table2Id"/>
<one-to-many class="com.abc.MyTable1" />
</set>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-MyTable2.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
Mapping for MyTable3 is :
Code:
<?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="com.abc.MyTable3" table="mytable3" dynamic-update="false" dynamic-insert="false">
<id name="table3Id" column="table3_id" type="java.lang.String" unsaved-value="any">
<generator class="assigned"></generator>
</id>
<property name="table3ColOne" type="java.lang.String" update="true" insert="true" column="column_3_1"/>
<property name="table3ColTwo" type="java.lang.String" update="true" insert="true" column="column_3_2"/>
<set name="tableOneItems3">
<key column="table3Id"/>
<one-to-many class="com.abc.MyTable1" />
</set>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-MyTable3.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
I have written the MyTable1PK class as follows
Code:
public class MyTable1PK implements Serializable {
private String table1Id;
private MyTable2 myTable2;
private MyTable3 myTable3;
/* Setter-Getter methods for the three fields */
public String toString() {
// call to apache commons implementation
}
public boolean equals ( Object second ) {
// call to apache commons implementation
}
public int hashCode () {
// call to apache commons implementation
}
}
and the code I am using for loading the MyTable1 object is
Code:
MyTable1PK pk1 = new MyTable1PK();
pk1.setTable1Id( "8" );
pk1.setMyTable2( new MyTable2("100"));
pk1.setMyTable3( new MyTable3("1000"));
System.out.println("Beginning Transaction");
Transaction tx = session.beginTransaction();
MyTable1 myTable1 = (MyTable1) session.load( MyTable1.class, pk1);
System.out.println("Ending Transaction");
tx.commit();
Can anyone help me in dealing with this error. I tried documentation and forums but couldn't find a solution for this.
TIA,