-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: net.sf.hibernate.TransientObjectException: object references
PostPosted: Thu Apr 29, 2004 4:06 pm 
Newbie

Joined: Thu Apr 29, 2004 3:31 pm
Posts: 3
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,


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 29, 2004 4:22 pm 
Newbie

Joined: Thu Apr 29, 2004 3:31 pm
Posts: 3
The first line in the exception trace of previuos mail has typo

net.sf.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.abc.model.MyTable2

The qualified name for MyTable2 class is com.abc.MyTable2

TIA,


Top
 Profile  
 
 Post subject: found a working solution
PostPosted: Fri Apr 30, 2004 11:18 am 
Newbie

Joined: Thu Apr 29, 2004 3:31 pm
Posts: 3
Ok I solved the problem myself.

If anyone is also having same problem, the mistake I did is highlighted in the below 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();

I replaced those lines with the lines in blue.


Transaction tx = session.beginTransaction();

MyTable2 table2Object = (MyTable2)sesion.load( MyTable2.class, "100" );
MyTable3 table3Object = (MyTable3)sesion.load( MyTable3.class, "100" );


MyTable1PK pk1 = new MyTable1PK();
pk1.setTable1Id( "8" );
pk1.setMyTable2( table2Object );
pk1.setMyTable3( table3Object );

MyTable1 myTable1 = (MyTable1) session.load( MyTable1.class, pk1);
System.out.println("Ending Transaction");
tx.commit();

Though I got the code to work, I am still concerned with calls for loading the other two objects by calling session.load(..). Is there a way to let hibernate load those objects.

TIA,


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.