-->
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: hibernate updating, not inserting on cascade of parent
PostPosted: Mon Aug 22, 2005 4:19 pm 
Beginner
Beginner

Joined: Tue Jun 28, 2005 4:33 pm
Posts: 21
Hi,

This should be a simple problem, but it's driving me crazy. I have a parent class with a one-to-one unidirectional association to another class. Primary keys are the same, but are assigned, so I need to set them manually in each class.

mapping in the parent class is:

<one-to one
name="child"
class="xxx.xxx.child"
cascade="all"
outer-join="auto"
</>

There is no association mapping in the child since it's unidirectional.

My problem is that when starting out both parent and child are transient. I set the child in the parent, and do a session save. Looking at the SQL, hibernate is doing an insert on the parent object, but it does an update on the child object. Since the child object is transient too, there is nothing to update and the child class isn't persisted; the SQL needs to be an insert, not an update.

Anyone have any idea how to make this work via a cascade (don't want to save each one separately).


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 22, 2005 4:25 pm 
Newbie

Joined: Tue Aug 02, 2005 1:40 pm
Posts: 16
Location: US
just add and unsaved-value="any" to the id of the child and this solves the problem. If it still does not the post the mapping files and pojo's and the code where you are tyring to set the child

_________________
Rajeshwar Rao

Please don't forget to give credit, if this posting helped to solve your problem.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 8:57 am 
Beginner
Beginner

Joined: Tue Jun 28, 2005 4:33 pm
Posts: 21
Thanks, but unsaved-value wasn't a attribute of one-to-one (got a mapping exception).

Forgot to mention I'm using version 2.18.

What I can't understand, is that I'm specifically calling a save() method, not an update() or a saveOrUpdate(). One would think that would force hibernate to use a SQL insert on all the cascaded objects too.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 9:10 am 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
The unsaved-value is an attribute of the "id" tag, not the "one-to-one". You need to set the unsaved-value attribute on the id tag for the child. You should setup the unsaved-value to have a value that will be the equivalent of "uninitialized" in ur application. For e.g. if i have an employee class, i would know that no employee will have id = -1 , so id set my id to have an attribute of
unsaved-value = -1

HTH


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 9:36 am 
Beginner
Beginner

Joined: Tue Jun 28, 2005 4:33 pm
Posts: 21
Thanks, that did the trick.

I'll need to dig up an explanation of what the unsaved-value is actually doing.

It was only a work around, but I found that using a saveOrUpdateCopy also solved the problem, but since that method uses extra calls to the database (it does a select before hand to figure out if the object is transient or persistent), it would have increased overhead.

Thanks again.


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