tags:

views:

449

answers:

1

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?

+1  A: 

It turned out to be easy...

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