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.  [ 3 posts ] 
Author Message
 Post subject: Adding existing object to a new parent.
PostPosted: Mon Apr 09, 2007 6:32 pm 
Newbie

Joined: Mon Jan 23, 2006 11:44 pm
Posts: 15
Location: New Jersey, US
Hi,

I have a system that deals with two classes. They form a parent-children relationship. The children are created first and later I want to add them to a new parent. The mapping files look like:

child:
<set name="_items"
table="Item"
lazy="true"
inverse="true"
cascade="update">
<key column="BatchId"></key>
<one-to-many class="Batch"></one-to-many>
</set>

parent:

<many-to-one
name="_batch"
class="Batch"
cascade="none"
outer-join="auto"
insert="true"
update="true"
column="BatchId"
not-null="false" />

Testing code looks like:

Batch batch = new Batch();
batch.Number = "2_string";
batch.NumberOfBills = 2;
batch.Total = 50;

Bill bill = (Bill)session.Load(typeof(Bill), 3);

bill.Batch = this;
batch.Bills.Add(bill);

session.Save(batch);

When i creat a parent and add existing children to it then save it, only the parent is persisted to database. The "BatchId" in the child record never get updated.

The generated sql only show one insert.

NHibernate: INSERT INTO CareBatch (NumberOfBills, Total, Number) VALUES (@p0, @p1, @p2); select SCOPE_IDENTITY()
@p0 = '2'
@p1 = '50'
@p2 = '2_string'

what did i do worng?
Thank a lot.

Hongze


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 09, 2007 7:38 pm 
Newbie

Joined: Mon Apr 02, 2007 12:31 pm
Posts: 19
Try changing the cascade element in the parent mapping, like this:

parent:
<many-to-one
name="_batch"
class="Batch"
cascade="save-update"
outer-join="auto"
insert="true"
update="true"
column="BatchId"
not-null="false" />

I think that will make the child object be persisted when the parent is saved.


Top
 Profile  
 
 Post subject: Does it only make the parent updated when we save the child?
PostPosted: Mon Apr 09, 2007 10:54 pm 
Newbie

Joined: Mon Jan 23, 2006 11:44 pm
Posts: 15
Location: New Jersey, US
Hi JWLato,

Thanks for replying. I will try it tomorrow but it looks to me that only makes the parent saved or updated when we SaveOrUpdate(the child). I will let you know the result.

Regards,

Hongze


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