Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate - WHERE EXISTS(X)

Tags:

nhibernate

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?

like image 866
Diego Mijelshon Avatar asked Dec 02 '25 22:12

Diego Mijelshon


1 Answers

It turned out to be easy...

var query = session.CreateQuery(
            "select distinct oi.Product from OrderItem oi");
return query.List<Product>();
like image 181
Diego Mijelshon Avatar answered Dec 06 '25 11:12

Diego Mijelshon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!