-->
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.  [ 7 posts ] 
Author Message
 Post subject: Child is not saved automatically .
PostPosted: Tue Jun 15, 2004 3:05 am 
Beginner
Beginner

Joined: Sun Nov 16, 2003 9:30 am
Posts: 20
Hi all,

I am trying to save a new object as follow :

Idea idea = new Idea();
idea.setXXX();
idea.setAttachments(new ArrayList());
Attachment a1 = new Attachment();
a1.setXXX();
idea.getAttachments().add(new Attachment());
mySession.save(idea)

only idea is saved by a1 is not saved. Is this the expected behaviour or a1 should be saved automatically ?

and idea to a1 is mono-directional.
Thanks.

Perseus

Here is my post.hbm.xml
============================================
<hibernate-mapping package="com.beez.idea.model">
<class name="Post" table="T_POST">
<id name="id" type="long">
<generator class="identity"/>
</id>

<property name="Subject" column="Subject" type="string"/>

<joined-subclass name="Idea" table="T_IDEA">
<key column="POST_ID"/>
<property name="Price" column="PRICE" type="double" not-null="true"/>
<bag name="Attachments" lazy="true">
<key column="ATTACHED_TO_ID"/>
<one-to-many class="com.beez.idea.model.Attachment"/>
</bag>
</joined-subclass>
<joined-subclass name="Attachment" table="T_ATTACHMENT">
<key column="POST_ID"/>
<property name="FileName" column="FILE_NAME" type="string" not-null="true"/>
</joined-subclass>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 15, 2004 7:55 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
have you read (at least twice) parent child relationship in the reference doc?

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 15, 2004 10:46 am 
Beginner
Beginner

Joined: Sun Nov 16, 2003 9:30 am
Posts: 20
Hi all ,

I now added attributes of cascade="save-update" and it's updated already correctly. However, I don't know why the T_ATTACHMENT.ATTACHED_TO_ID has null inserted.

Thanks.

Perseus


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 15, 2004 10:50 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
read the reference doc and wiki another time ;)

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 15, 2004 10:11 pm 
Beginner
Beginner

Joined: Sun Nov 16, 2003 9:30 am
Posts: 20
Hi all ,

Most documentation are about the bi-directional and I found rare exmamples on directional one. Of course I know that if inverse="true" and I have to set the parent in the child side. But just don't quite understand if there is not inverse="true" as in my case and the parent should be maintaining the relationship and why's my code still cannot update the ATTACHED_TO_ID.

Please advise. Thanks.

Perseus


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 16, 2004 2:34 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
save child first, add it to collection (parent); save parent

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 16, 2004 8:18 am 
Beginner
Beginner

Joined: Sun Nov 16, 2003 9:30 am
Posts: 20
Thanks a lot !

Why do I have to save the child first with cascade="save-update" in order
to have foreign key of the child created also ? Isn't that parent should be the only side maintaining the relationship?


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