-->
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: Child records are not inserted with assigned id's
PostPosted: Wed Feb 15, 2006 3:51 pm 
Newbie

Joined: Sun Jul 10, 2005 3:11 pm
Posts: 1
Hibernate version: 3.1.2

Name and version of the database you are using:
Oracle 10G Rel 2

I'm using sample code to insert a parent / child record into the database by using the following mapping

<?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 package="com.test.common.cache.entry">
<class name="ParentHistEntry" table="PSPARENTHIST">
<id name="id" column="ID" unsaved-value="0">
<generator class="assigned"/>
</id>
<bag name="children" cascade="all">
<key column="PARENTID" not-null="true" />
<one-to-many class="ChildHistEntry" />
</bag>
<property name="parentName" column="PARENTNAME" type="string"/>
</class>
<class name="ChildHistEntry" table="PSCHILDHIST">
<id name="id" column="ID" unsaved-value="0">
<generator class="assigned"/>
</id>
<bag name="grandChildren" cascade="all">
<key column="CHILDID" not-null="true"/>
<one-to-many class="GrandChildHistEntry"/>
</bag>
<property name="childName" column="CHILDNAME"/>
</class>
<class name="GrandChildHistEntry" table="PSGRANDCHILDHIST">
<id name="id" column="ID" unsaved-value="0">
<generator class="assigned"/>
</id>
<property name="grandChildName" column="GRANDCHILDNAME"/>
</class>
</hibernate-mapping>

I set the ID's for the parent and all child records.
The parent get's inserted, but the children do not get inserted.
If I change the generator for the child table to native and do not set an id, the child record get's inserted into the database.

How do I set id's for the child and not have to explicitly save the child record using session.save(child). The following is a code excerpt

ParentEntry l_parent = new ParentEntry();
l_parent.setId(1);
.... (set's attribs for parent)
ChildEntry l_child = new ChildEntry();
l_child.setId(2);
List l_list = new ArrayList();
l_list.add(l_child);
l_parent.setChildren(l_list);

Session l_session = HibernateManager.currentSession();
Transaction l_tx = l_session.beginTransaction();
l_session.save(l_parent);
l_tx.commit();

Please let me know what I need to change to get this to work.

Thanks
Bharat


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 15, 2006 8:41 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Use the version attribute - otherwise you need to call save.
see http://www.hibernate.org/hib_docs/v3/reference/en/html/mapping.html#mapping-declaration-version


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.