Nhibernate and not-exists query

Posted by Dan on Stack Overflow See other posts from Stack Overflow or by Dan
Published on 2010-05-06T10:45:34Z Indexed on 2010/05/06 18:28 UTC
Read the original article Hit count: 168

Filed under:

I'm trying to construct a query in NHibernate to return a list of customers with no orders matching a specific criteria.

My Customer object contains a set of Orders:

<set name="Orders">
    <key column="CustomerID" />
    <one-to-many class="Order" />
</set>

How do I contruct a query using NHibernate's ICriteria API to get a list of all customers who have no orders? Using native SQL, I am able to represent the query like this:

select * from tblCustomers c where not exists 
    (select 1 from tblOrders o where c.ID = o.CustomerID)

I have been unable to figure out how to do this using aliases and DetatchedCriteria objects. Any guidance would be appreciated!

Thanks!

© Stack Overflow or respective owner

Related posts about nhibernate