-->
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.  [ 4 posts ] 
Author Message
 Post subject: Cascade insert fails in one-to-many...
PostPosted: Tue Nov 18, 2003 6:32 pm 
Beginner
Beginner

Joined: Tue Nov 18, 2003 12:34 am
Posts: 39
Location: Dallas, Texas, US
Hi,
I have a problem with cascaded insert in the following example.

//------------------ Mapping - parent ---------------------------
<class
name="ParentImpl"
table="Parent"
dynamic-update="true"
dynamic-insert="true"
>

<id
name="id"
column="Parent_Id"
type="string"
>
<generator class="assigned">
</generator>
</id>

<bag
name="children"
table="Children"
lazy="false"
inverse="true"
cascade="all"
>

<key
column="Parent_Id"
/>

<one-to-many
class="ChildImpl"
/>
</bag>


//----------------------- Mapping - child --------------------
<class
name="ChildImpl"
table="Child"
dynamic-update="false"
dynamic-insert="false"
>

<id
name="id"
column="id"
type="string"
>
<generator class="assigned">
</generator>
</id>

<many-to-one
name="parent"
class="ParentImpl"
cascade="all"
outer-join="auto"
update="true"
insert="true"
column="Parent_Id"
not-null="true"
/>

//--------------------------------
In the Hibernate code, I have something like the following:
....
defaultParent = createParent("Kitzrow's");

Child diff = createChild("Diff");

...
Session session = persistLayerInstance.createSession();

persistLayerInstance.saveSession(session, defaultParent);

diff.setParent(defaultParent);
defaultParent.add(diff);

session.flush();
session.connection.commit();
...........

//---------------------------------------------
Now, Hibernate creates an "Insert" for the parent, but for the child it tries to do an "Update". Since the child row does not exist, it throws an error.

Now, I am trying to understand why is Hibernate doing an "update" when it is supposed to do an "insert". Any insights much appreciated...
Thanks,
Madhan.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 18, 2003 7:10 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Please read the documentation about unsaved-value. NOtice that for an assigned id, you would need to use unsaved-value="any". This is a FAQ.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 18, 2003 9:14 pm 
Beginner
Beginner

Joined: Tue Nov 18, 2003 12:34 am
Posts: 39
Location: Dallas, Texas, US
Thanks a lot! I did read it before, but did not make any sense to me. Now, I understand. Thanks again.
Madhan.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 18, 2003 9:46 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Great :) Good Luck!


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