-->
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.  [ 7 posts ] 
Author Message
 Post subject: saving child objects
PostPosted: Tue Jan 27, 2004 1:40 pm 
Beginner
Beginner

Joined: Tue Oct 07, 2003 12:11 pm
Posts: 32
Location: US
my objectA is mapped to tableA
my objectA has a set (java.util.Set) that set has objectB
my objectB is mapped to tableB

I am doing a session.save(objectA);

will my data in objectB saved during the same call?

my mapping file is like this



Code:
<class name="ObjectA" schema="APP" table="tabA">
<id column="TYPE_CODE_OID" name="id" type="java.lang.Long">
<generator class="assigned"/>
</id>
<property column="TAB_A" length="5" name="typeCode" not-null="true" type="java.lang.Short"/>
<property column="APPL_VARIABLE" length="255" name="applVariable" type="java.lang.String"/>
<property column="PATH" length="255" name="path" not-null="true" type="java.lang.String"/>
<property column="SEQ_NUM" length="5" name="seqNum" not-null="true" type="java.lang.Short"/>
<property column="ACTIVE_FLAG" length="5" name="activeFlag" not-null="true" type="java.lang.Short"/>
<property column="MODIFIABLE_FLAG" length="5" name="modifiableFlag" not-null="true" type="java.lang.Short"/>
<property column="MODIFIED_DATETIME" length="29" name="modifiedDatetime" not-null="true" type="java.util.Date"/>
<property column="MODIFIED_DATETIME1" name="modifiedDatetime1" not-null="true" type="java.util.Date"/>
<property column="MODIFIER_ID" length="30" name="modifierId" not-null="true" type="java.lang.String"/>
    <set name="typeCodeText" lazy="false" inverse="true">
        <key column = "TAB_A_OID"/>
        <one-to-many class="ObjectB"/>
    </set>
</class>
<class name="ObjectB" schema="APP" table="tabB">
<composite-id>
<key-property column="LOCALE" length="30" name="locale" type="java.lang.String"/>
<key-property column="TAB_A_OID" length="19" name="typeCodeOid" type="java.lang.Long"/>
</composite-id>
<property column="ABBR_DISPLAY_TEXT" length="60" name="abbrDisplayText" type="java.lang.String"/>
<property column="DISPLAY_TEXT" length="255" name="displayText" type="java.lang.String"/>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 1:43 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
only with cascade settings for the set.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 2:52 pm 
Beginner
Beginner

Joined: Tue Oct 07, 2003 12:11 pm
Posts: 32
Location: US
Sorry I am not familiar with cascade settings. How do I do that in my config file.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 2:55 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Read the "Parent/child relationship" chapter of the documentation.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 5:20 pm 
Beginner
Beginner

Joined: Tue Oct 07, 2003 12:11 pm
Posts: 32
Location: US
I saw the "Parent/child relationship" chapter and added cascade="all" in my mapping file. I am using a <one-to-many /> tag in ObjectA
I am not using <many-to-one... /> in the ObjectB

I get an error. When I try to create a new entry, it seems to be doing alright with the ObjectA. But it seems to be updating the ObjectB instead of inserting and then throws an exception.

Here is the debug log for Hibernate classes.
Code:
2004-01-27 14:54:55,373 DEBUG (SetTypeCodesCmd.java:433) - Setting TypeCode OID = 400001
2004-01-27 14:54:55,373 DEBUG (SetTypeCodesCmd.java:447) - Found TypeCodeText. Setting TypeCode OID = 400001
2004-01-27 14:54:55,393 DEBUG (EntityPersister.java:464) - Inserting entity: com.gtech.service.typecode.TypeCode#400001
2004-01-27 14:54:55,413 DEBUG (EntityPersister.java:366) - Dehydrating entity: com.gtech.service.typecode.TypeCode#400001
2004-01-27 14:54:55,453 DEBUG (EntityPersister.java:617) - Updating entity: com.gtech.service.typecode.TypeCodeText#com.gtech.service.typecode.TypeCodeText@6d9e6c[locale=en_US,typeCodeOid=400001]
2004-01-27 14:54:55,483 DEBUG (EntityPersister.java:366) - Dehydrating entity: com.gtech.service.typecode.TypeCodeText#com.gtech.service.typecode.TypeCodeText@6d9e6c[locale=en_US,typeCodeOid=400001]



Code:
<class name="com.gtech.service.typecode.TypeCode" table="MS_TYPE_CODE2">
   <id column="TYPE_CODE_OID" name="id" type="java.lang.Long">
      <generator class="assigned"/>
   </id>
   <property column="TYPE_CODE" length="5" name="typeCode" not-null="true" type="java.lang.Short"/>
   <property column="APPL_VARIABLE" length="255" name="applVariable" type="java.lang.String"/>
   <property column="PATH" length="255" name="path" not-null="true" type="java.lang.String"/>
   <property column="SEQ_NUM" length="5" name="seqNum" not-null="true" type="java.lang.Short"/>
   <property column="ACTIVE_FLAG" length="5" name="activeFlag" not-null="true" type="java.lang.Short"/>
   <property column="MODIFIABLE_FLAG" length="5" name="modifiableFlag" not-null="true" type="java.lang.Short"/>
   <property column="MODIFIED_DATETIME" length="29" name="modifiedDatetime" not-null="true" type="java.util.Date"/>
   <property column="MODIFIER_ID" length="30" name="modifierId" not-null="true" type="java.lang.String"/>
    <set name="typeCodeText" lazy="false" inverse="false" cascade="all">
        <key column="TYPE_CODE_OID"/>
        <one-to-many class="com.gtech.service.typecode.TypeCodeText"/>
    </set>
</class>
<class name="com.gtech.service.typecode.TypeCodeText" table="MS_TYPE_CODE_TEXT2">
   <composite-id>
      <key-property column="LOCALE" length="30" name="locale" type="java.lang.String"/>
      <key-property column="TYPE_CODE_OID" length="19" name="typeCodeOid" type="java.lang.Long"/>
   </composite-id>
   <property column="ABBR_DISPLAY_TEXT" length="60" name="abbrDisplayText" type="java.lang.String"/>
   <property column="DISPLAY_TEXT" length="255" name="displayText" type="java.lang.String"/>
</class>



Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 5:33 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Hibernate can't distinquish between new and old instances if you use assigned or composite identifiers, thus it can't cascade proberly. Use explicit save() or update() calls, or implement an Interceptor.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 5:36 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
Hibernate can't distinquish between new and old instances if you use assigned or composite identifiers


Well, not unless you add a version property.


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