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
|