How to filter child collection in JPQL query?

戏子无情 提交于 2019-12-07 08:04:10

问题


I've the following DB model:

Category -< ProductCategory >- Product -< Variant

(Category has many-to-many relationship with Product and Product has one-to-many relationship with Variant)

Now I need to get all Category records that have product with active variants. I'm getting these objects via the following JPQL query:

@Query("select distinct c from Category c join c.products as p join p.variants as pv where pv.active = true")

It works well - returns categories accurately - however every single Category contains all the products - not only these with active variants.

How can I filter out the products (or variants) that are inactive in a single query?

Here's a postgres script that with database struct and sample data. For given data two categories (CAT 1, CAT 2), two products (PROD 1, PROD 2) and three variants (VAR 1, VAR 2, VAR 3) should be returned.


回答1:


I had exactly the same problem and it took me a while to find out how this works. The child list should be filtered when you add FETCH after your JOIN like this:

SELECT DISTINCT c FROM Category c JOIN FETCH c.products as p join p.variants as pv where pv.active = true



回答2:


I have the same problem on it, and I found that the FetchType in the @OneToMany annotation is important. It need to be set as Lazy mode. If it is Eager mode, jpa will create the sql query to fetch the data from the table for your child collection and won't filter it for you.



来源:https://stackoverflow.com/questions/40847016/how-to-filter-child-collection-in-jpql-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!