Silly Question Re Updates with children

0 views
Skip to first unread message

codemonkey

unread,
Sep 13, 2008, 9:34:12 AM9/13/08
to nhusers
Another question, might be a bit silly but my brain is fried today,
Say I have 2 tables A and B, with a one to many between them:

A
--
Id
Name

B
---
Id
AId
Name


If I do something like so:

A a = new A();
a.Id = 1;
a.Name = "name";
a.BItems = null;

// THEN UPDATE a

Even though the collection is set to null, the children still remain,
am I missing something here? If I were to load A then call
a.BItems.Clear() and update they are removed as I expect.


Cheers

Dario Quintana

unread,
Sep 13, 2008, 11:24:50 AM9/13/08
to nhu...@googlegroups.com
Hello

Could you please add the mappings ?
--
Dario Quintana
http://darioquintana.com.ar

codemonkey

unread,
Sep 13, 2008, 11:36:53 AM9/13/08
to nhusers
No worries, mappings below:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="A" table="A" lazy="false">
<id name="Id" column="ID" type="int">
<generator class="native" />
</id>
<property name="Name" column="Name" type="string" />
<set name="BItems" inverse="true" lazy="true" cascade="delete-
orphan">
<key column="AId" />
<one-to-many class="B" />
</set>
</class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="B" table="B" lazy="false">
<id name="Id" column="ID" type="int">
<generator class="native" />
</id>
<property name="Name" column="Name" type="string" />
<many-to-one name="A" column="AId" cascade="save-update" not-
null="true" />
</class>
</hibernate-mapping>



Thanks
Stefan


On Sep 13, 11:24 pm, "Dario Quintana" <conta...@darioquintana.com.ar>
wrote:
> Hello
> Could you please add the mappings ?
>

Eric Hauser

unread,
Sep 13, 2008, 1:03:06 PM9/13/08
to nhusers
You can't update the inverse end of a bidirectional relationship. See
the docs:

http://www.hibernate.org/hib_docs/nhibernate/html/collections.html

"Changes made only to the inverse end of the association are not
persisted. This means that NHibernate has two representations in
memory for every bidirectional association, one link from A to B and
another link from B to A. This is easier to understand if you think
about the .NET object model and how we create a many-to-many
relationship in C#:"

Dario Quintana

unread,
Sep 13, 2008, 4:16:43 PM9/13/08
to nhu...@googlegroups.com
Here you have another explanation about the inverse option.
http://simoes.org/docs/hibernate-2.1/155.html

codemonkey

unread,
Sep 13, 2008, 8:18:11 PM9/13/08
to nhusers
So what is my answer here to set inverse="false" or am I missing the
point, even with inverse="False" I get the same behaviour.

codemonkey

unread,
Sep 13, 2008, 8:51:39 PM9/13/08
to nhusers
Sorry I was wrong here, with inverse="False" it seems to detach the
child by setting its key to null. Which I assume you would excpect.
Reply all
Reply to author
Forward
0 new messages