-->
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: Persisting a Parent/Child Object Graph
PostPosted: Tue Oct 04, 2005 9:09 pm 
Newbie

Joined: Wed Sep 14, 2005 9:15 am
Posts: 12
Location: St. Louis
I'm looking to save a basic Parent/Child object graph, where the Parent's Primary Key is not determined before the saving the Parent or the Child records (I'm using MySQL's auto increment feature that gives the next Primary Key for the Parent record upon record insert).

With this in mind, when Hibernate attempts to persist the entire object graph, it successfully inserts the Parent record, but fails on the Child records b/c the Child records do not contain the Primary Key value of the Parent (I obviously cannot set this value while constructing my Child domain object b/c it is unknown until record insert).

My Child table contains a compound Primary Key, which uses the Parent's Primary Key as one of it's key values - hope that makes sense. The Child table is basically just an association table.

I am using
Code:
<hibernate-mapping default-cascade="all">
in my Parent .hbm doc.

I am also using
Code:
<generator class="select" />
in my Parent .hbm doc.

Lastly, I'm also setting the Parent record into each Child record (per the documentation).

Any idea how to tell Hibernate to use the Primary Key of the Parent as part of the compound Primary Key of the Child? I hope all this makes sense... I think I've read it back to myself about 50 times to make sure it sounds correct...


Top
 Profile  
 
 Post subject: you don't need to have id, but reference
PostPosted: Tue Oct 04, 2005 10:38 pm 
Newbie

Joined: Mon Jan 24, 2005 6:05 pm
Posts: 12
parent mapping
<set name="authProfileSet" inverse="true" cascade="save-update">
<key column="user_id"/>
<one-to-many class="AuthProfile"/>
</set>

child mapping
<many-to-one name="person" column="user_id" class="Person" not-null="true" />

so, you might have
person.setAuthProfile(Set authProfile)

now, you can extend your parent class with method that add the child.

public void addAuthProfile(AuthProfile authProfile) {
Set<AuthProfile> authProfileSet = getAuthProfileSet();
if (authProfileSet == null) {
authProfileSet = new TreeSet<AuthProfile>();
setAuthProfileSet(authProfileSet);
}
authProfile.setPerson(this);
authProfileSet.add(authProfile);
}

when you save the person, the save action should cascade to the child. I haven't test it out, let me know if it works.
thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 05, 2005 10:25 pm 
Newbie

Joined: Wed Sep 14, 2005 9:15 am
Posts: 12
Location: St. Louis
Well, I gave that a shot, but it didn't seem to work...

Not sure where to go from here, so I thought I'd post my code. As you'll see, the hbm docs below were created by the Hibernate Tools. I reverse engineered the database to create the hbm docs and the POJO's - pretty damn sweet! - but maybe that's part of my problem - not sure...

Here's the Parent .hbm doc:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-cascade="all">
<!--
        Auto-generated mapping file from
        the hibernate.org cfg2hbm engine
-->
    <class name="com.test.domain.Items" table="items" catalog="devschema">
        <id name="itemId" type="integer">
            <column name="item_id" />
            <generator class="select" />
        </id>
        <many-to-one name="users" class="com.test.domain.Users">
            <column name="username" length="45" not-null="true" />
        </many-to-one>
        <property name="title" type="string">
            <column name="title" length="60" not-null="true" />
        </property>
        <property name="description" type="string">
            <column name="description" length="65535" />
        </property>
        <set name="itemsPaymentTypeses" inverse="true">
            <key>
                <column name="item_id" not-null="true" />
            </key>
            <one-to-many class="com.hypobid.domain.ItemsPaymentTypes" />
        </set>
    </class>
</hibernate-mapping>


And the Child .hbm doc:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!--
        Auto-generated mapping file from
        the hibernate.org cfg2hbm engine
-->
    <class name="com.test.domain.ItemsPaymentTypes" table="items_payment_types" catalog="devschema">
        <composite-id name="id" class="com.test.domain.ItemsPaymentTypesId">
            <key-property name="itemId" type="integer">
                <column name="item_id" />
            </key-property>
            <key-property name="paymentTypeId" type="integer">
                <column name="payment_type_id" />
            </key-property>
        </composite-id>
        <many-to-one name="items" class="com.test.domain.Items" update="false" insert="false">
            <column name="item_id" not-null="true" />
        </many-to-one>
        <many-to-one name="paymentTypes" class="com.test.domain.PaymentTypes" update="false" insert="false">
            <column name="payment_type_id" not-null="true" />
        </many-to-one>
    </class>
</hibernate-mapping>


And the PaymentTypes .hbm doc:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!--
        Auto-generated mapping file from
        the hibernate.org cfg2hbm engine
-->
    <class name="com.test.domain.PaymentTypes" table="payment_types" catalog="devschema">
        <id name="paymentTypeId" type="integer">
            <column name="payment_type_id" />
            <generator class="assigned" />
        </id>
        <property name="paymentTypeName" type="string">
            <column name="payment_type_name" length="45" not-null="true" />
        </property>
        <set name="itemsPaymentTypeses" inverse="true">
            <key>
                <column name="payment_type_id" not-null="true" />
            </key>
            <one-to-many class="com.test.domain.ItemsPaymentTypes" />
        </set>
    </class>
</hibernate-mapping>


And the code to create the Object Graph in my Controller:

Code:
      
Items item = (Items) command;
      UserSession userSession = (UserSession)request.getSession().getAttribute("userSession");
      item.setUsers(userSession.getUser());
      Set mySet = new HashSet();
      String[] paymentTypes = request.getParameterValues("itemsPaymentTypes");
      for (int i = 0; i < paymentTypes.length; i++) {
         ItemsPaymentTypesId itemsPaymentTypesId = new ItemsPaymentTypesId();
         itemsPaymentTypesId.setPaymentTypeId(new Integer(paymentTypes[i]));
         ItemsPaymentTypes itemsPaymentTypes = new ItemsPaymentTypes(itemsPaymentTypesId);
         itemsPaymentTypes.setItems(item);
         mySet.add(itemsPaymentTypes);
      }
      item.setItemsPaymentTypeses(mySet);
itemService.saveOrUpdateItem(item);


And part of the stack trace:

Code:
005-10-05 21:21:35,859 DEBUG [org.hibernate.util.JDBCExceptionReporter] - Could not execute JDBC batch update [insert into devschema.items_payment_types (item_id, payment_type_id) values (?, ?)]
java.sql.BatchUpdateException: Column 'item_id' cannot be null
   at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:642)
   at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:294)
   at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:193)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:230)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)


So it appears as if Hibernate does not automatically set the Parent's Primary Key (i.e., item_id) to the Child record's composite Primary Key...

Any help is greatly, greatly appreciated, as this one has had me stumped!

Thanks a lot,

Matt


Top
 Profile  
 
 Post subject: Persisting a Parent/Child Object Graph
PostPosted: Thu Oct 06, 2005 9:19 am 
Newbie

Joined: Wed Sep 14, 2005 9:15 am
Posts: 12
Location: St. Louis
Oh yeah, one more thing.... In my controller, I also tried the following:

ItemsPaymentTypesId itemsPaymentTypesId = new ItemsPaymentTypesId();
itemsPaymentTypesId.setPaymentTypeId(new Integer(paymentTypes[i]));
itemsPaymentTypesId.setItemId(item.getItemId());
ItemsPaymentTypes itemsPaymentTypes = new ItemsPaymentTypes(itemsPaymentTypesId);
itemsPaymentTypes.setItems(item);
mySet.add(itemsPaymentTypes);

But that didn't work either... I thought upon insert of the Parent, Hibernate would propagate the Parent's Primary Key down to each Child record automatically. This seems so basic - I just don't understand why this is isn't working...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 01, 2008 11:06 am 
Beginner
Beginner

Joined: Tue Jan 08, 2008 2:15 pm
Posts: 22
I have the same problem. Have you fixed yours? If so, how?


Top
 Profile  
 
 Post subject: Re: Persisting a Parent/Child Object Graph
PostPosted: Wed Aug 26, 2009 5:53 am 
Newbie

Joined: Wed Aug 26, 2009 5:39 am
Posts: 1
I too have hit upon this problem. Can you please post how you resolved this issue?


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.