Hi!
I am new to NHibernate, so my question may be trivial. I have Parent-Child relationship (as one-to-many). I am trying to fetch all Parent objects with their Children. Currently, I have two parents in database, one with single child, the other with two children.
NHibernate version: 1.0.4.0
Mapping documents:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="BusinessEntity.Parent, BusinessEntity" table="Parent">
<id name="Id" column="ParentId" type="Int32" unsaved-value="0">
<generator class="assigned" />
</id>
<property name="Name"/>
<property name="Gender"/>
<list name="Children" fetch="select">
<key column ="ParentId" />
<index column="ChildId" />
<one-to-many class="BusinessEntity.Child, BusinessEntity"/>
</list>
</class>
</hibernate-mapping>
<class name="BusinessEntity.Child, BusinessEntity" table="Child">
<id name="Id" column="ChildId" type="Int32" unsaved-value="0">
<generator class="assigned" />
</id>
<property name="Name"/>
<property name="Age"/>
<property name="ParentId"/>
</class>
Both Parent and child implement matching interfaces:
Code:
public interface IParent
{
int Id
{
get;
set;
}
string Name
{
get;
set;
}
char Gender
{
get;
set;
}
IList Children
{
get;
set;
}
}
public interface IChild
{
int Id
{
get;
set;
}
string Name
{
get;
set;
}
ushort Age
{
get;
set;
}
int ParentId
{
get;
set;
}
}
Code between sessionFactory.openSession() and session.close():Code:
tx = session.BeginTransaction();
ICriteria criteria = session.CreateCriteria(typeof(Parent));
parents = criteria.List();
session.Close();
I fetch parents and first has collection of 4 children (with two NULL values) and the other 3 children (with two NULL values).
What seems to be the problem? I know I could use set (and ISet) instead of list (and IList), but I am trying to make it work with System.Collection namespace.
Thanks.