-->
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.  [ 6 posts ] 
Author Message
 Post subject: Automatic save for the referenced object
PostPosted: Thu May 06, 2004 9:54 am 
Beginner
Beginner

Joined: Tue Mar 02, 2004 1:12 pm
Posts: 25
Location: Newcastle Upon Tyne
HI

Suppose I have two objects, A and B with a one-to-many mapping between them. So, I'd have the HBM mapping as:

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="uk.org.mygrid.mir.infomodel.beans.Person" table="person">
      <id name="LSID" type="java.lang.String" column="person_id" length="80">
         <generator class="assigned"/>
      </id>
      <property name="firstName" type="java.lang.String" column="fName" not-null="true" length="20"/>
      <property name="lastName" type="java.lang.String" column="lName" not-null="true" length="30"/>
      <property name="email" type="java.lang.String" column="email" not-null="true" length="80"/>
      <property name="x509DN" type="java.lang.String" column="x509dn" not-null="true" length="50"/>   
      <!-- bi-directional one-to-many association to LabBook (one person can have multiple LabBooks) -->
      <array name="labBooks" inverse="true" cascade="all">
         <key>
            <column name="person_id"/>
         </key>
         <index column="lab_ind"/>
         <one-to-many class="uk.org.mygrid.mir.infomodel.beans.LabBookView"/>
      </array>            
   </class>
</hibernate-mapping>


and

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="uk.org.mygrid.mir.infomodel.beans.LabBookView" table="labbookview">
      <id name="LSID" type="java.lang.String" column="labbookview_id" length="80">
         <generator class="uuid.string"/>
      </id>
      <property name="name" type="java.lang.String" column="name" length="30"/>
      <property name="rule" type="java.lang.String" column="rule" length="50"/>
      <!-- associations -->
      <!-- bi-directional many-to-one association to Person (creator of the labbook) -->
      <many-to-one name="creator" class="uk.org.mygrid.mir.infomodel.beans.Person" not-null="true">
         <column name="person_id"/>
      </many-to-one>
   </class>
</hibernate-mapping>


Now, I can create a labBook object, add it to the array in the person object and save the person object - this in turn saves the labBook object automatically - provided the ID field in the labBook object IS NOT of "assigned" type.

But, if I try to create a person object, and associate it with a newly created labBook object and try to save the labBook object, it gives me an exception:

Code:
     [java] net.sf.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing:
uk.org.mygrid.mir.infomodel.beans.Person
     [java]     at net.sf.hibernate.impl.SessionImpl.throwTransientObjectException(SessionImpl.java:2734)
     [java]     at net.sf.hibernate.impl.SessionImpl.getEntityIdentifierIfNotUnsaved(SessionImpl.java:2726)
      [java]     at net.sf.hibernate.type.EntityType.getIdentifier(EntityType.java:66)
     [java]     at net.sf.hibernate.type.EntityType.isDirty(EntityType.java:164)
      [java]     at net.sf.hibernate.type.TypeFactory.findDirty(TypeFactory.java:225)
      [java]     at net.sf.hibernate.persister.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:268)
     [java]     at net.sf.hibernate.impl.SessionImpl.flushEntity(SessionImpl.java:2472)
     [java]     at net.sf.hibernate.impl.SessionImpl.flushEntities(SessionImpl.java:2422)
     [java]     at net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2224)
     [java]     at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2203)
     [java]     at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
     [java]     at uk.org.mygrid.mir.test.HibernateTest.createTestData(HibernateTest.java:135)
     [java]     at uk.org.mygrid.mir.test.HibernateTest.main(HibernateTest.java:242)
     [java] java.lang.RuntimeException: couldn't get connection
     [java]     at uk.org.mygrid.mir.test.HibernateTest.main(HibernateTest.java:248)
     [java] Exception in thread "main"


It asks me to save the person object first. Can I get the same effect as the first one I described (automatic save) in this case also? And 2ndly, is there any way to get this effect with the ID field defined as ASSIGNED?

Regards
Arijit


Top
 Profile  
 
 Post subject: Re: Automatic save for the referenced object
PostPosted: Fri May 07, 2004 5:26 am 
Beginner
Beginner

Joined: Tue Mar 02, 2004 1:12 pm
Posts: 25
Location: Newcastle Upon Tyne
arijitm wrote:
...It asks me to save the person object first. Can I get the same effect as the first one I described (automatic save) in this case also? And 2ndly, is there any way to get this effect with the ID field defined as ASSIGNED?

Regards
Arijit


As an addendum to the previous topic: supposing I have a one-to-many relationship between a person and a labbook. If I delete one person object, would the associated labbook objects be deleted as well? It isn't working that way in practice though.....

Arijit


Top
 Profile  
 
 Post subject: Re: Automatic save for the referenced object
PostPosted: Fri May 07, 2004 7:31 am 
Beginner
Beginner

Joined: Tue Mar 02, 2004 1:12 pm
Posts: 25
Location: Newcastle Upon Tyne
arijitm wrote:
arijitm wrote:
...It asks me to save the person object first. Can I get the same effect as the first one I described (automatic save) in this case also? And 2ndly, is there any way to get this effect with the ID field defined as ASSIGNED?

Regards
Arijit


As an addendum to the previous topic: supposing I have a one-to-many relationship between a person and a labbook. If I delete one person object, would the associated labbook objects be deleted as well? It isn't working that way in practice though.....

Arijit


No, things have all messed up. Using the same mapping files described above (changed only the id part of the labBook object to assigned, as that is what I need), I can't get the child object (a labBook) saved when the parent (a person) is saved. I am getting the following error:

[java] Assgined LSID: urn:lsid:mygrid.org.uk:labbookview:5f3768eb80f094ea00e3849c4f914edb
[java] - Could not synchronize database state with session
[java] net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
[java] net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
[java] at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
[java] at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
[java] at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:688)
[java] at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:688)
[java] at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:641)
[java] at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:641)
[java] at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
[java] at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
[java] at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2382)
[java] at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2382)
[java] at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2336)
[java] at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2336)
[java] at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2204)
[java] at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2204)
[java] at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
[java] at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
[java] at uk.org.mygrid.mir.test.HibernateTest.createTestData(HibernateTest.java:142)
[java] at uk.org.mygrid.mir.test.HibernateTest.main(HibernateTest.java:249)
[java] at uk.org.mygrid.mir.test.HibernateTest.createTestData(HibernateTest.java:142)
[java] java.lang.RuntimeException: couldn't get connection
[java] at uk.org.mygrid.mir.test.HibernateTest.main(HibernateTest.java:249)
[java] at uk.org.mygrid.mir.test.HibernateTest.main(HibernateTest.java:255)
[java] Exception in thread "main"

Please give me some hints. I got to get past this issue.

Arijit


Top
 Profile  
 
 Post subject: Re: Automatic save for the referenced object
PostPosted: Fri May 07, 2004 8:07 am 
Beginner
Beginner

Joined: Tue Mar 02, 2004 1:12 pm
Posts: 25
Location: Newcastle Upon Tyne
Hi Again

This is what I found: if I update these relations at the 1-end (in this example, the person object - i.e. add the labBooks in the array contained inside the person object), the cascade-delete is working when I delete a person object. But it doesn't work if I add a reference to the person object in the labBook object (and don't use the array on the other end).

If I don't save a labBook explicitly, and try to add it in the array and then save the parent person object, the cascade-save isn't working. I am getting the exception mentioned in the previous post.

Is this behaviour correct?

Arijit


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 07, 2004 9:05 am 
Regular
Regular

Joined: Mon Nov 24, 2003 6:36 pm
Posts: 105
The last error you posted is typically the result of HIbernate not knowing whether to send an update or insert, because it can't use your identifier to know that (normally if id==null, hib does an insert, otherwise, does an update)

To get around that prob, you could use the <timestamp> property....
basically tell hibernate to use a different field to determine if the object is new or existing.

James


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 07, 2004 9:15 am 
Beginner
Beginner

Joined: Tue Mar 02, 2004 1:12 pm
Posts: 25
Location: Newcastle Upon Tyne
jlawmi wrote:
The last error you posted is typically the result of HIbernate not knowing whether to send an update or insert, because it can't use your identifier to know that (normally if id==null, hib does an insert, otherwise, does an update)

To get around that prob, you could use the <timestamp> property....
basically tell hibernate to use a different field to determine if the object is new or existing.

James


Oops - that solved one part of the puzzle...thanx a ton James

Arijit


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.