tags:

views:

1600

answers:

3

I have two classes that are associated with a one-to-one mapping:

<class name="Employee" table="Employees">
  ...
  <one-to-one name="Address" class="AddressInfo">
  ...
</class>

I would like to use a criteria expression to get only Employees where the the associated Address class is not null, something like this (which I know doesn't work):

IList employeesWithAddresses = sess.CreateCriteria(typeof(Employee))
    .Add( Expression.IsNotNull("Address") )
    .List();

I guess this is either a really difficult question or almost no one has tried to do this?

A: 

What type of database are you using? What is the actual query being sent to the database?

Loophole
+2  A: 

Have you tried creating an alias for the Address property and checking if the ID/primary key of the Address is not null?

Something like:

IList employeesWithAddresses = sess.CreateCriteria(typeof(Employee))
    .CreateCriteria("Address", "address").Add( Expression.IsNotNull("Id") )
    .List();
ChrisAnnODell
A: 

To implicitly trigger a Alias/Join for one-to-one, shouldn't you use dot-notation? And then checking any field in your Address-class:

IList employeesWithAddresses = sess.CreateCriteria(typeof(Employee))
    .Add( Expression.IsNotNull("Address.Id") )
    .List();

This is probably the same approach as Kareena, but I believe this is equal to .CreateAlias(). With CreateAlias you can specify FetchMode (the type of join used) but since you want rows with corresponding Addresses, that shouldn't be nessecary (because INNER is default). Nor should it be nessecary to check if it's null.

IList employeesWithAddresses = sess.CreateCriteria(typeof(Employee))
    .CreateAlias("Address")
    .List();
jishi