I have three entities with many to many relationship between them.
Packages <--> join table <--> ProtectedItems <--> join table <--> ContentItems.
I want to query all Packages
that are associated with some Content Items
. Currently i am using a native sql query with inner joins.
SELECT ci.stream_id, p.package_id, p.package_name FROM packages p
INNER JOIN jt_packages_protected_items jtppi
ON p.account_id=jtppi.p_account_id AND p.package_id=jtppi.package_id
INNER JOIN protected_items pi
ON jtppi.pi_account_id=pi.account_id AND jtppi.protected_item_id=pi.protected_item_id
INNER JOIN jt_protected_items_stream_mappings jtpism
ON pi.account_id=jtpism.pi_account_id AND pi.protected_item_id=jtpism.protected_item_id
INNER JOIN content_items ci
ON jtpism.ci_account_id=ci.account_id AND jtpism.content_id_extension=ci.content_id_extension
How can i convert the above native query to jpql query. Any help will be greatly appreciated. Basically i want to know how to join multiple tables in JPQL.
Edit following answer from @Eugene -
What if i want to also filter results by packageId.
I tried below -
1)This did not work - does not compile
@Query("SELECT p2 from (SELECT p FROM Packages p WHERE p.packageId.packageId=1) as p2 inner join pp.protectedItems pi inner join pi.streamMappings ci WHERE ci.streamId=:streamId")
2)This worked - but is this the right way and is this optimized.
@Query("SELECT p FROM Packages p inner join p.protectedItems pi inner join pi.streamMappings ci WHERE ci.streamId=:streamId AND p.packageId.packageId=:packageId")
If there is a better way - then plz suggest..