Hi all,
I am new to NHibernate and currently trying to persuade our architect to use NHibernate to replace our terrible in-house developed DAL.
However, I have got a problems in the little sample I created for testing one-to-many.
Firstly, here's the .hbm.xml
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Entities" namespace="Entities">
<class name="Parent" table="parents">
<!-- primary key -->
<id name="ParentID" column="parentid" type="Int32">
<generator class="native"/>
</id>
<!-- properties -->
<property name="ParentProperty1" column="parentproperty1" />
<property name="ParentProperty2" column="parentproperty2" />
<bag name="Children" table="children" inverse="true" cascade="all-delete-orphan">
<key column="parentid"/>
<one-to-many class="Child"/>
</bag>
</class>
<class name="Child" table="children">
<!-- primary key -->
<id name="ChildID" column="childid" type="Int32">
<generator class="native"/>
</id>
<!-- properties -->
<property name="ChildProperty1" column="childproperty1" />
<property name="ChildProperty2" column="childproperty2" />
</class>
</hibernate-mapping>
Now the entity classes
Code:
namespace Entities
{
[Serializable]
public class Child
{
private int childID;
private string childProperty1;
private string childProperty2;
public virtual int ChildID
{
get { return childID; }
set { childID = value; }
}
public virtual string ChildProperty1
{
get { return childProperty1; }
set { childProperty1 = value; }
}
public virtual string ChildProperty2
{
get { return childProperty2; }
set { childProperty2 = value; }
}
}
[Serializable]
public class Parent
{
private int parentID;
private string parentProperty1;
private string parentProperty2;
private IList children;
public virtual int ParentID
{
get { return parentID; }
set { parentID = value; }
}
public virtual string ParentProperty1
{
get { return parentProperty1; }
set { parentProperty1 = value; }
}
public virtual string ParentProperty2
{
get { return parentProperty2; }
set { parentProperty2 = value; }
}
public virtual IList Children
{
get { return children; }
set { children = value; }
}
}
}
Now when I ran following lines of code, the parent got saved to database successfully but for some reason child1 is not saved to database (i.e. the children table is empty).
Code:
Parent parent = new Parent();
parent.ParentProperty1 = "pp1";
parent.ParentProperty2 = "pp2";
parent.Children = new ArrayList();
session.Save(parent);
Child child1 = new Child();
child1.ChildProperty1 = "cp1";
child1.ChildProperty2 = "cp2";
parent.Children.Add(child1);
session.SaveOrUpdate(parent);
I tried to remove the 'session.Save(parent);', so now the client code looks like this:
Code:
Parent parent = new Parent();
parent.ParentProperty1 = "pp1";
parent.ParentProperty2 = "pp2";
parent.Children = new ArrayList();
Child child1 = new Child();
child1.ChildProperty1 = "cp1";
child1.ChildProperty2 = "cp2";
parent.Children.Add(child1);
session.SaveOrUpdate(parent);
Now both the 'parent' and 'child1' got saved to database successfully, however, the 'parentid' column of 'child1' is NULL.
To summarise my question, can anyone tell me why:
1. Saving the parent first then add children and save again doesn't save the children properly.
2. how come the 'parentid' of children table is always null even though I have added them to an Parent object.