Problem
I am using a three level object model where the second level subclass represents a bi-directional self referencing set of data.
The Parent ( Mother ) doesn't hyrate properly in this situation. While it has the values of the actual record all the member variables are null or default.
Code:
. . . . . . +-------+ . . . . .
. . . . . . |Entity | . . . . .
. . . . . . | . . . | . . . . .
. . . . . . +-------+ . . . . .
. . . . . . . .^. . . . . . . .
. . . . . . . .|. .+----+ . . .
. . . . . . +-------+ . | . . .
. . . . . . |Animal | . | . . .
. . . . . . | . . . |<--+ . . .
. . . . . . +-------+ . . . . .
. . . . . . . .^. . . . . . . .
. . . . . . . .|. . . . . . . .
. . . . +------+------+ . . . .
. . . . | . . . . . . | . . . .
. . . +---+ . . . . +---+ . . .
. . . |Cat| . . . . |Dog| . . .
. . . +---+ . . . . +---+ . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
NHibernate version: 1.2.0 beta 3 Name and version of the database I am using: SQL 2005 SP1Mapping documents:Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Entity.Core.Entity,Entity.Core" table="Entity" lazy="false">
<id name="Key" column="EntityId" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>
<property column="IsActive" type="Boolean" name="IsActive" not-null="true" />
<joined-subclass name="Entity.Core.Animal,Entity.Core" table="Animal">
<key column="EntityId"/>
<property column="Name" type="String" name="Name" length="50" />
<bag name="Children" inverse="true" lazy="false" >
<key column="Mother" />
<one-to-many class="Entity.Core.Animal,Entity.Core" />
</bag>
<many-to-one name="Mother" cascade="none" column="Mother" class="Entity.Core.Animal,Entity.Core" />
<joined-subclass name="Entity.Core.Cat,Entity.Core" table="Cat">
<key column="EntityId"/>
</joined-subclass>
<joined-subclass name="Entity.Core.Dog,Entity.Core" table="Dog">
<key column="EntityId"/>
</joined-subclass>
</joined-subclass>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
protected void Page_Load(object sender, EventArgs e)
{
ISession session = NHibernateSessionManager.Instance.GetSession();
ITransaction tx = session.BeginTransaction();
IQuery query = session.CreateQuery("select a from Entity as a where a.Key = :id");
query.SetCharacter("id", '7');
foreach (Core.Entity entity in query.Enumerable())
{
// This casts just fine
Animal animal = entity as Animal;
// Animal happens to be of type Cat
if (animal != null)
{
// Cat cast to Dog = null normally a *good* but I suspect for the wrong reasons so *bad*
Dog dogParent = animal.Mother as Dog;
// Cat cast to Cat = null *bad*
Cat catParent = animal.Mother as Cat;
// Cat cast to Dog = null *good*
Dog dogCurrent = animal as Dog;
// Cat cast to Cat = not null *good*
Cat catCurrent = animal as Cat;
// Dog cast to Dog = not null *good*
Dog dogChild = animal.Children[0] as Dog;
// Dog cast to Cat = null *good*
Cat catChild = animal.Children[0] as Cat;
}
}
tx.Commit();
session.Close();
}