You see this post very beatiful at
http://hassan.behzadian.com/nhibernate-problem.htm
Hi.
I have a problem using NHibernate.
I have a class named Parent, this class contains a (property) collection (of type Iesi.Collections.ISet - ) named Children that contains references to list of other object type named Child. also child has a property named Parent that is a reference to its parent object. below is their definitions:
namespace NHT
{
public class Parent
{
private string title;
public string Title
{
get
{
return title;
}
set
{
title = value;
}
}
private Guid id;
public Guid Id
{
get
{
return id;
}
set
{
id = value;
}
}
private ISet children = new Iesi.Collections.ListSet();
public ISet Children
{
get
{
return children;
}
set
{
children = value;
}
}
}
}
namespace NHT
{
public class Child
{
private string title;
public string Title
{
get
{
return title;
}
set
{
title = value;
}
}
private Guid id;
public Guid Id
{
get
{
return id;
}
set
{
id = value;
}
}
private Parent parent;
public Parent Parent
{
get
{
return parent;
}
set
{
parent = value;
}
}
}
}
I mapped them to database like below
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="NHT.Parent, NHT" table="ORM_Parent">
<id name="Id" column="ParentId" type="System.Guid" unsaved-value="">
<generator class="guid" />
</id>
<property name="Title" type="string" />
<set name="Children" >
<key column="ParentId"/>
<one-to-many class="NHT.Child, NHT"/>
</set>
</class>
</hibernate-mapping>
----------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="NHT.Child, NHT" table="ORM_Child">
<id name="Id" column="ChildId" type="System.Guid" unsaved-value="">
<generator class="guid" />
</id>
<property name="Title" column="title" type="string"></property>
<many-to-one name="Parent" class="NHT.Parent, NHT" column="ParentId" />
</class>
</hibernate-mapping>
I write below code to test it,
-------------------
Parent parent = new Parent();
parent.Title = "Parent Title";
for (int i = 0; i < 15; i++) {
HibernateTestClassLibrary.Child child = new HibernateTestClassLibrary.Child();
child.Title = "Child Title:" + i;
parent.Children.Add(child);
child.Parent = parent;
//DatabaseManager.CurrentSession.Save(content);
}
object key = DatabaseManager.CurrentSession.Save(parent);
DatabaseManager.CurrentSession.Flush();
Response.Write("Object saved:" + key.ToString());
--------------------------------
but when executed give below error message:
-----------------
SQL insert, update or delete failed (expected affected row count: 1, actual affected row count: 0). Possible causes: the row was modified or deleted by another user, or a trigger is reporting misleading row count.
-----------------
I monitored Sql Server, Nhibernate first inserts parent and tries to update child object (in database), but because no child was saved (and then no records will be changed), throws above exception if i uncomment
//DatabaseManager.CurrentSession.Save(content);
all things will be true (child objects will be saved and updated by NHibernate) but i did not sure to get object loaded correctly if i change set in parent class like below
<set name="Children" invers="true" >
<key column="ParentId"/>
<one-to-many class="NHT.Child, NHT"/>
</set>
and NHibernate will tries to update Child objects then no error will be made. Now my question how i can configure NHibernate to save all child objects just by saving parent object?