-->
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.  [ 5 posts ] 
Author Message
 Post subject: Continued problems with parent-child relationships
PostPosted: Fri Sep 05, 2003 2:06 pm 
Newbie

Joined: Thu Sep 04, 2003 2:30 pm
Posts: 4
OKay, I am still struggling with the problem of persisting a parent and the child is not being persisted. I have read Chapter 8 of the docs dealing with Parent Child Relationships (like 4 times!), and in fact I am trying to execute the exact example illustrated in that chapter. I am not having much success, however.

Hibernate.cfg.xml
<hibernate-configuration>

<session-factory>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/quickstart</property>
<property name="show_sql">true</property>


<!-- Mapping files -->
<mapping resource="Parent.hbm.xml"/>
<mapping resource="Child.hbm.xml"/>

</session-factory>

</hibernate-configuration>

Parent.hbm.xml
<hibernate-mapping>
<class name="Parent" table="parent">
<id name="id">
<generator class="identity"/>
</id>
<property name="name"/>
<set name="children" inverse="true" cascade="all">
<key column="parent_id"/>
<one-to-many class="Child"/>
</set>
</class>
</hibernate-mapping>

Child.hbm.xml
<hibernate-mapping>

<class name="Child" table="child">
<id name="id">
<generator class="identity"/>
</id>
<property name="name"/>
<many-to-one name="parent" column="parent_id" not-null="true"/>
</class>
</hibernate-mapping>

Test Code

Configuration cfg = new Configuration().configure("/hibernate.cfg.xml");
SessionFactory sf =cfg.buildSessionFactory();

Session sess = sf.openSession();
Parent p = (Parent) sess.load(Parent.class, new Integer(1));
Child c = new Child();
c.setName("Sam");
p.addChild(c);
sess.flush();
sess.close();

I do not get any exception, but nothing seems to happen. When I have the "show-sql" flag turned on, I see...

Hibernate: update child set name=?, parent_id=? where id=?

However, shouldn't it be an insert? I am thoroughly confused as to what is going on, so any help is greatly appreciated. I feel like I have followed the example in the docs exactly, but I obviously must be missing something. I have not provided the implementations of the Parent and Child classes, but can easily do so if needed. Thanks for any help!

Bryan


Top
 Profile  
 
 Post subject: a few things to try
PostPosted: Fri Sep 05, 2003 2:15 pm 
Newbie

Joined: Fri Sep 05, 2003 12:01 pm
Posts: 5
It doesn't look like you are saving anything in your main code loop. Just before you do session.flush() try adding:
Code:
session.saveOrUpdate(p)


Additionally you will need to specify cascade="all" or one of the other cascade options if you want the child persisted when you persist the parent. Otherwise you will have to add the above call for the child as well.
Code:
session.saveOrUpdate(c)


Top
 Profile  
 
 Post subject: Thank you for the prompt reply
PostPosted: Fri Sep 05, 2003 2:28 pm 
Newbie

Joined: Thu Sep 04, 2003 2:30 pm
Posts: 4
Above in Person.hmb.xml I have ....

Code:
       <set name="children" inverse="true" cascade="all">
            <key column="parent_id"/>
            <one-to-many class="Child"/>
        </set>


Now according to the documentation from the Parent Child Relationships chapter....

Code:
8.3. Cascades

The explicit call to save() is still annoying. We will address this by using cascades.

<set name="children" inverse="true" cascade="all">
    <key column="parent_id"/>
    <one-to-many class="Child"/>
</set>

This simplifies the code above to

Parent p = (Parent) session.load(Parent.class, pid);
Child c = new Child();
p.addChild(c);
session.flush();


I read this to mean that I should not need to explicitly call save() on either the parent or the child. Am I reading this documentation wrong? Perhaps I have the cascade in the wrong place? Thanks again for the help.

Bryan


Top
 Profile  
 
 Post subject: add unsaved-value="0" to you mapping files for id
PostPosted: Fri Sep 05, 2003 9:16 pm 
Newbie

Joined: Tue Aug 26, 2003 7:56 am
Posts: 4
Location: Chongqing,China
add unsaved-value="0" to you mapping files for id column


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 09, 2003 11:43 am 
Beginner
Beginner

Joined: Wed Aug 27, 2003 6:44 pm
Posts: 20
>>I read this to mean that I should not need to explicitly call save() on >>either the parent or the child.

I think you need to call explict save on the parent object.

Correct me if i am wrong.


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