Diego Mijelshon
Diego Mijelshon

Reputation: 52753

NHibernate - WHERE EXISTS(X)

I have the following tables (simplified):

Product(Id, Name)
OrderItem(Id, ProductId)

...which map to the following classes:

Product {Id, Name}
OrderItem {Id, Product (many-to-one)}

I need the (N)Hibernate syntax to retrieve the Products that appear in Orders.
The SQL would be something like:

select *
from   Product
where  exists (
       select *
       from   OrderItem
       where  OrderItem.ProductId = Product.Id)

How do I create the Criteria?

Upvotes: 3

Views: 1279

Answers (1)

Diego Mijelshon
Diego Mijelshon

Reputation: 52753

It turned out to be easy...

var query = session.CreateQuery(
            "select distinct oi.Product from OrderItem oi");
return query.List<Product>();

Upvotes: 3

Related Questions