-->
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.  [ 1 post ] 
Author Message
 Post subject: NHibernate parent-child relationship modified items issue
PostPosted: Tue Jul 26, 2011 5:01 am 
Newbie

Joined: Tue Jul 26, 2011 4:55 am
Posts: 2
Hello,

I have the following Parent-Child relationship model:

public class Parent
{
public virtual int? ID { get; set; }
public virtual string Name { get; set; }

public virtual IList<Child> Child
{
get;
set;
}
}

public class Child
{
public virtual int? ID { get; set; }
public virtual string Name { get; set; }
}

<class name="Parent" table="Parent">
<id name="ID" column="ID" type="int" >
<generator class="sequence">
<param name="sequence">SQ_PARENT</param>
</generator>
</id>
<property column="PNAME" name="Name" />
<bag name="Child" cascade="all-delete-orphan">
<key column="PARENTID"/>
<one-to-many
class="Child, NHibernateOneToMany"/>
</bag>
</class>

<class name="Child" table="Child">
<id name="ID" column="ID" type="int" >
<generator class="sequence">
<param name="sequence">SQ_CHILD</param>
</generator>
</id>
<property column="CNAME" name="Name" />
</class>

And 2 test methods.

[TestMethod()]
public void Save_Parent()
{
IParentRepository repository = new ParentRepository();
Parent dbProduct = new Parent()
{
Name = "P5",
};
dbProduct.Child = new List<Child>();
dbProduct.Child.Add(new Child() { Name = "A_C_1" });
dbProduct.Child.Add(new Child() { Name = "A_C_2" });

repository.Add(dbProduct);
}

[TestMethod()]
public void Save_Parent_2()
{
Parent x = new Parent()
{
ID = 44,
Name = "P5"
};
x.Child = new List<Child>();
x.Child.Add(new Child() { Name = "A_C_NEW" });
IParentRepository repository = new ParentRepository();
repository.Add(x);
}

In the second test, I assigned my parent the ID that resulted on the first test.

The db outcome is:
* one parent in db
* one linked child with the name A_C_NEW
* 2 unlinked childs with names: A_C_1, A_C_2 (the PARENT_ID column is null)

I would like to know if there is any way of deleting the unlinked childs.

The logic above models the persistence logic of my app. I have a parent-child tree in the database, that is serialized by a WCF Service and sent to a WPF client that edits the parent-child tree (removes childs, adds childs) and afterwards the client serializes the tree and sends it back to the WCF that should save it to the database.

Thank you,
Andrei


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.