NHibernate - WHERE EXIST (X)

I have the following tables (simplified):

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

... which belong 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 the Order.
SQL will look something like this:

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

How to create criteria?

+3
source share
1 answer

It turned out to be easy ...

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

Source: https://habr.com/ru/post/1702643/


All Articles