ssmithstone
ssmithstone

Reputation: 1209

jpa query where the entity on a collection is in a list

Probably not best described in the title but i have 3 entities

Order -> OneToMany -> OrderProduct

and

OrderProduct <- ManyToOne -> Product

Product <- OneToMany -> OrderProduct ,

my native query that works is

SELECT t0.*
FROM isc_orders t0 
INNER JOIN isc_customers t1 ON t0.ordcustid = t1.customerid 
INNER JOIN isc_order_products t2 ON t0.orderid = t2.orderorderid 
INNER JOIN isc_products t3 ON t2.ordprodid = t3.productid
where t3.productid in (359, 344, 345, 346, 347, 348)

is there any way to do this in a JPA way in a single select as I have my collections and and entities eager loading with join so that the object graph is populated on one pass

Upvotes: 2

Views: 2075

Answers (1)

axtavt
axtavt

Reputation: 242786

Something like this:

SELECT DISTINCT o
FROM Order o JOIN o.orderProducts op1
JOIN FETCH o.orderProducts op2 JOIN FETCH op2.product
WHERE op1.product.productid IN ?

Note that you need two joins with OrderProducts - one for filtering, another for eager fetching.

Upvotes: 1

Related Questions