This is the mapping that i am using. Note that both planner and employee maps to the exact same table.
by using this function:
Code:
public T GetObjectByUniqueKey<T>(string field, string value)
{
return (T)Session.CreateCriteria(typeof(T)).
Add(NHibernate.Expression.Expression.Eq(field, value)).UniqueResult();
}
and passing in e.g. 'planner' as the type i get null back because multiple records was returned??
to 'fix this problem i do the following:
Code:
static public T LookUpByEmployeeNBNo<T>(object NBNo)
{
//HACK: problem with nhibernate returning all subclasses on list
if (NBNo == null) return default(T);
IList employees = repository.Session.CreateCriteria(typeof(T)).
Add(NHibernate.Expression.Expression.Eq("NBNo", NBNo)).List();
if (employees.Count > 0)
{
foreach (object obj in employees)
if (obj.GetType() == typeof(T)) return (T)obj;
}
return default(T);
}
looking at the Ilist result i look for an exact match of the type 'Planner' and then return that object
Key point is that planner inherits from employee however a employee is not a planner therefore only a planner should be returned!!
please help if any got an answer thanks!
Code:
<class name="Planner" table="Employee">
<id name="ID" column="ID">
<generator class="assigned" />
</id>
<property column="status" name="_status"></property>
<many-to-one name="Branch" column="BranchID" class="Branch" />
<property name="SurName" column="SurName" type="String" />
<property name="NBNo" column="NBNo" type="String"/>
<set name="Roles" cascade="all" >
<key column="EmpId"/>
<one-to-many class="EmployeeRole"/>
</set>
</class>
<!-- Employee -->
<class name="Employee" table="Employee">
<id name="ID" column="ID">
<generator class="assigned" />
</id>
<property column="status" name="_status"></property>
<many-to-one name="Branch" column="BranchID" class="Branch" />
<property name="SurName" column="SurName" type="String" />
<property name="NBNo" column="NBNo" type="String"/>
<set name="Roles" cascade="all" >
<key column="EmpId"/>
<one-to-many class="EmployeeRole"/>
</set>
</class>