-->
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.  [ 2 posts ] 
Author Message
 Post subject: bi-directional CRUD not working
PostPosted: Tue Jul 28, 2009 9:43 pm 
Beginner
Beginner

Joined: Thu Jul 22, 2004 10:51 pm
Posts: 29
Location: sydney australia
Hi All,

I'm attempting to use a bi-directional relationship (one-to-many) parent to child, whereby when I save (saveOrUpdate) the parent, the children are also CRUDed (inserted/updated/deleted). I'm pretty sure I've set it up correctly, however when I add an element to the children set, no new records appear in the database.

Parent:
Code:
<hibernate-mapping>
   <class name="package.ParentObject" table="parent_object">
      <id name="id">
         <generator class="sequence">
            <param name="sequence">seq_parent_object_id</param>
         </generator>
      </id>
      <set name="children" inverse="true" lazy="false" order-by="id">
         <key column="ParentObjectId" not-null="true"/>
         <one-to-many class="package.ChildObject"/>
      </set>
      <property name="childCount"/>
   </class>
</hibernate-mapping>
Child:
Code:
<hibernate-mapping>
   <class name="package.ChildObject" table="child_object">
      <id name="id">
         <generator class="sequence">
            <param name="sequence">seq_child_object_id</param>
         </generator>
      </id>
      <many-to-one name="ParentObject" class="package.ParentObject" column="parentObjectId" lazy="false"/>
      <property name="name"/>
   </class>
</hibernate-mapping>
Usage:
Code:
Session s = sessionFactory.getCurrentSession();
Transaction t = s.beginTransaction();
ParentObject parent = (ParentObject) s.get(ParentObject.class, new Long(1));
if (parent.getChildren().isEmpty())
{
   ChildObject child = new ChildObject();
   child.setParentObject(parent);
   child.setName("test");
   parent.getChildren().add(child);
}
parent.setChildCount(parent.getChildren().size());
s.saveOrUpdate(parent);
t.commit();


after this operation, SELECT * FROM child_object WHERE parentObjectId=1 returns 0 records, while SELECT childCount FROM parent_object WHERE id=1 returns 1, meaning the parent was updated but the new child was not inserted.

Is there anything obviously wrong with what I'm doing?

NOTE: I have removed what I consider unrelated code/fields which may be causing the problem, however if they are the cause then hibernate needs some better error reporting.


Top
 Profile  
 
 Post subject: Re: bi-directional CRUD not working
PostPosted: Tue Jul 28, 2009 10:42 pm 
Beginner
Beginner

Joined: Thu Jul 22, 2004 10:51 pm
Posts: 29
Location: sydney australia
missing cascade="all"
Code:
<hibernate-mapping>
   <class name="package.ParentObject" table="parent_object">
      <id name="id">
         <generator class="sequence">
            <param name="sequence">seq_parent_object_id</param>
         </generator>
      </id>
      <set name="children" cascade="all" inverse="true" lazy="false" order-by="id">
         <key column="ParentObjectId" not-null="true"/>
         <one-to-many class="package.ChildObject"/>
      </set>
      <property name="childCount"/>
   </class>
</hibernate-mapping>


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