2
votes

In nhibernate, I have two classes that are associated with a many-to-one mapping:

<class name="Employee" table="Employee">
  ..
  <bag name="orgUnits">
    <key column="id" />
    <one-to-many name="OrgUnit" class="OrgUnit">
  </bag>
  ..
</class>

I would like to use a criteria expression to get only Employees where the the collection is null (ie no orgunits) , something like this :

IList employeesWithNoOrgUnit = sess.CreateCriteria(typeof(Employee))
    .Add( Expression.IsNull("OrgUnits") )
    .List();

This doesn't filter the collection as I expect.

1
Is OrgUnits a collection ? If that's the case shouldn't it be a one-to-many relation from Employee side ? - Beatles1692

1 Answers

5
votes

Colleague just found one way that works.

IList employeesWithNoOrgUnit = sess.CreateCriteria(typeof(Employee))
    .Add( Restrictions.IsEmpty("OrgUnits") )
    .List();