I'm going nuts trying to get NHibernate do one of the most basic things - automatically having a collection property of a persisted "parent" object populated with associated "child" objects. I can load the parent object from the database using ISession.Load, but when I access the set of children, it's always empty, even though records exist in the database.
I have an Oracle database that has "Campus" and "Building" tables. A Building is associated with exactly one Campus via a foreign CampusId key in the Building table, and a Campus can have many Buildings. Right now I just have one campus with ID 7 and two buildings with CampusId = 7.
I
have successfully been able to create both Campus and Buildings with parent-child associations and persist them using NHibernate. But I haven't been able to just fetch the collection of Buildings on a Campus object - it's always empty when I load a Campus.
My entity classes look like this:
Code:
public class Building
{
private long buildingId;
private Campus campus;
private string buildingName;
public Building()
{
}
public long BuildingId
{
get { return buildingId; }
set { buildingId = value; }
}
public Campus Campus
{
get { return campus; }
set { campus = value; }
}
public string BuildingName
{
get { return buildingName; }
set { buildingName = value; }
}
}
public class Campus
{
private long campusId;
private string campusName;
private ISet buildings;
public Campus()
{
}
public long CampusId
{
get { return campusId; }
set { campusId = value; }
}
public string CampusName
{
get { return campusName; }
set { campusName = value; }
}
public ISet Buildings
{
get { return buildings; }
set { buildings = value; }
}
public void AddBuilding( Building b )
{
if( Buildings == null )
Buildings = new HashedSet();
Buildings.Add( b );
b.Campus = this;
}
}
The mapping files look like this:
Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="gbrownc.LockerService.Persistence.Building, gbrownc.LockerService" table="Building">
<id name="BuildingId" unsaved-value="0" type="Int64">
<generator class="native">
<param name="sequence">Building_seq</param>
</generator>
</id>
<many-to-one name="Campus" column="CampusId"/>
<property name="BuildingName" type="String" length="64"/>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="gbrownc.LockerService.Persistence.Campus, gbrownc.LockerService" table="Campus">
<id name="CampusId" unsaved-value="0" type="Int64">
<generator class="native">
<param name="sequence">Campus_seq</param>
</generator>
</id>
<property name="CampusName" type="String" length="64"/>
<set name="Buildings" inverse="true" lazy="false">
<key column="CampusId"/>
<one-to-many class="gbrownc.LockerService.Persistence.Building, gbrownc.LockerService"/>
</set>
</class>
</hibernate-mapping>
I'm just doing this:
Code:
Campus campus = session.Load( typeof( Campus ), 7 ) as Campus;
buildingCount = campus.Buildings.Count; // always 0! :(
I've tried several things: using an IList instead of ISet, playing around with the "lazy", "inverse", and "outer-join" settings, using an IEnumerator on the set instead of the Count property... Everything I've tried gives me a set (or list) with zero elements.
I must be making some really stupid mistake! Does anyone have any ideas?