-->
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: Paren't key value in children table is 0 upon creation
PostPosted: Tue Mar 14, 2006 9:40 am 
Newbie

Joined: Tue Mar 14, 2006 9:24 am
Posts: 1
Hello,

I have one hibernate object say Parent which is in one-to-many relationship with another object Child. When I create a new Parent and populate its properties I also create a Set of Child objects and assign them to Parent with a setter method. Then Parents gets persisted to db and is assigned an id via increment generator. But the problem is that Child's table also has a field referring the id of its Parent but at the time of creation of Parent and Child I don't know the id which Parent will be assigned to and thus the parent_id field in Child's table is set to 0 upon hbSession.save().
Is there any way to tell hibernate to assign the same parent_id key to the Child's parent_id field upon creation of these objects?

Thank you,
Andrew Stepanenko

Hibernate version: 3.0

Mapping documents:
<!-- Parent object -->
<hibernate-mapping>
<class name="Parent" table="int_reg_forms">
<id name="regId" column="reg_id" type="int" unsaved-value="null">
<generator class="increment"/>
</id>
... properties ...
<set name="courses" cascade="all" inverse="true" lazy="true">
<key column="intst_id"/>
<one-to-many class="Child"/>
</set>
</class>
</hibernate-mapping>

<!-- Child object-->

hibernate-mapping>
<class name="Child" table="intst_courses">
<id name="id" column="id" type="int" unsaved-value="null">
<generator class="increment"/>
</id>
<property name="intstId" column="intst_id" type="integer"
not-null="true"/>
<property name="courseId" column="course_id" type="integer"
not-null="true"/>
</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using: MySQL 4.1.7

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

_________________
Andrew Stepanenko,
Software engineer.
Ukrainian-Dutch Faculty of Economics and Management
Ternopil State Economic University,
UKRAINE,
Web: http://unf.tane.edu.ua


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 12:42 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
you don't want this in your child:

<property name="courseId" column="course_id" type="integer"
not-null="true"/>

You want
<many-to-one
name="parent_ref"
class="Parent"
column="reg_id"
/>

Then in your Child class create a member variable of type Parent named "parent_ref" and provide getters/setters for it.

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.