-->
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: one-to-one save cascade with hibernate 2.1
PostPosted: Wed Dec 24, 2003 1:36 pm 
Newbie

Joined: Wed Dec 24, 2003 6:37 am
Posts: 8
Hello,
I've read the docs and browsed the forums and still can't figure out this one:

I have a Parent and a Child in a one-to-one relationship, sharing the ID, which is actually created first in the Child object.
When I save the parent, it inserts the parent and then tries an update on the child (not yet inserted)...
///////////////////////////////////
Here is what I'm trying to do:

Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();

Parent p = new Parent();
p.setName("parentName");
Child c = new Child();
c.setObjectId("user1");
c.setName("childName");
c.setParent(p);
p.setChild(c);

session.save(p);

tx.commit();
HibernateUtil.closeSession();

////////////////////////////////////
My config goes like:
<class
name="xpetstore.domain.Parent"
table="T_PARENT"
>
<id name="objectId">
<generator class="foreign">
<param name="property">child</param>
</generator>
</id>

<one-to-one
name="child"
class="xpetstore.domain.Child"
cascade="all"
/>
<property
name="name"
type="java.lang.String"
column="name"
length="20"
not-null="false"
unique="false"
/>
</class>

<class
name="xpetstore.domain.Child"
table="T_CHILD"
>
<id name="objectId">
<generator class="assigned"/>
</id>

<property
name="name"
type="java.lang.String"
column="name"
length="20"
not-null="false"
unique="false"
/>
</class>
///////////////////////////////////////
Note.
right now in class Parent: getObjectId() returns getChild().getObjectId()...

I have tried several ways to do the cascade save but no chance yet.
I would deeply appreciate any help or directions on this one.
Thanks a lot again

Gilles


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 29, 2003 6:01 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Since you child has an assigned generator, you manually set a non null id, then Hibernate try to update it. It applies the saveOrUpdate strategy. The easiest solution is to manually save the child before saving the parent.
Have a look at the reference guide and http://www.hibernate.org/116.html#A11 for a list of solutions.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 30, 2003 6:25 am 
Newbie

Joined: Wed Dec 24, 2003 6:37 am
Posts: 8
Thanks Emmanuel,

I'm actually looking for a way to do it with just one "smart" save, would you know what could force the session.save(p) to save the child first?
I've got cascade="all", constrained="true" from Parent.
The one-to-one are now:
Parent:
<one-to-one
name="child"
class="xpetstore.domain.Child"
cascade="all"
constrained="true"
/>

Child:
<one-to-one
name="parent"
class="xpetstore.domain.Parent"
/>

I can't find any example that's close to my scenario.

Thanks again
Gilles


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 30, 2003 6:47 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
The cascaded and constrained stuff plus using unsaved-value="any" in the child id would do the work.

Child will be saved instead of updated, and parent will be saved after child.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 30, 2003 7:24 am 
Newbie

Joined: Wed Dec 24, 2003 6:37 am
Posts: 8
thank you , thank you, thank you...

the unsaved-value was my mistake...
back to documentation, I guess...

thanks a lot Emmanuel
Gilles


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