JPQL 查询:属于多个类别的项目
JPQL query: Item that belongs to multiple categories
有一个实体 Items,它有一个名为 categories 的集合字段,我需要找到至少属于两个特定类别的项目,但我找不到构建正确 JPQL 查询的方法。
@Entity
public class Items {
@ManyToMany (fetch = FetchType.EAGER)
private List<Category> categories;
}
我可以找到属于一个类别的项目:
SELECT i FROM Item i WHERE :cat MEMBER OF item.categories
我可以 select 项目与以下几个类别中的任何一个:
SELECT i FROM Item i WHERE :cat1 MEMBER OF item.categories OR :cat MEMBER OF item.categories
但是当我尝试 select 至少有两个特定类别的项目时,以下查询没有获得任何项目:
SELECT i FROM Item i WHERE :cat1 MEMBER OF item.categories AND :cat2 MEMBER OF item.categories
正确的做法是什么?
此致,
巴勃罗
我在使用 ObjectDB 时遇到了这个问题。
我也联系过他们,答案是这样的:
This seems to be the result of how JPQL queries are converted into SQL
like syntax before executing. MEMBER OF is implemented using JOIN with
a new synthetic variable for iteration over the collection. In your
query item.categories appears twice, but the same synthetic variable for
iterating over that collection is used for both occurrences, and that
variable cannot match both sides of the AND with the same value.
Possibly we may have to use separate iteration per MEMBER OF as the
results as demonstrated by your post seem unacceptable (although JQPL
itself has strange behaviour in some known cases due to the conversion
to JOIN). However, using separate variables may sometimes make the
query more complex and slow unnecessarily (e.g. for the OR query in
your post), so any change requires careful planning.
As a quick solution, you may replace AND of MEMBER OF with 2 explicit
JOIN variables for iteration over the collection with 2 independent
variables.
因此,解决方案是使用以下查询:
SELECT DISTINCT item
FROM Item item JOIN item.categories cat1 JOIN item.categories cat2
WHERE cat1 = :cat1 AND cat2 = :cat2
我不知道这是否只是这个特定 JPA 实现 (ObjectDB) 的问题。
不管怎样,如果有人有类似的问题,我希望这个post能帮到你。
有一个实体 Items,它有一个名为 categories 的集合字段,我需要找到至少属于两个特定类别的项目,但我找不到构建正确 JPQL 查询的方法。
@Entity
public class Items {
@ManyToMany (fetch = FetchType.EAGER)
private List<Category> categories;
}
我可以找到属于一个类别的项目:
SELECT i FROM Item i WHERE :cat MEMBER OF item.categories
我可以 select 项目与以下几个类别中的任何一个:
SELECT i FROM Item i WHERE :cat1 MEMBER OF item.categories OR :cat MEMBER OF item.categories
但是当我尝试 select 至少有两个特定类别的项目时,以下查询没有获得任何项目:
SELECT i FROM Item i WHERE :cat1 MEMBER OF item.categories AND :cat2 MEMBER OF item.categories
正确的做法是什么?
此致, 巴勃罗
我在使用 ObjectDB 时遇到了这个问题。 我也联系过他们,答案是这样的:
This seems to be the result of how JPQL queries are converted into SQL like syntax before executing. MEMBER OF is implemented using JOIN with a new synthetic variable for iteration over the collection. In your query item.categories appears twice, but the same synthetic variable for iterating over that collection is used for both occurrences, and that variable cannot match both sides of the AND with the same value.
Possibly we may have to use separate iteration per MEMBER OF as the results as demonstrated by your post seem unacceptable (although JQPL itself has strange behaviour in some known cases due to the conversion to JOIN). However, using separate variables may sometimes make the query more complex and slow unnecessarily (e.g. for the OR query in your post), so any change requires careful planning.
As a quick solution, you may replace AND of MEMBER OF with 2 explicit JOIN variables for iteration over the collection with 2 independent variables.
因此,解决方案是使用以下查询:
SELECT DISTINCT item
FROM Item item JOIN item.categories cat1 JOIN item.categories cat2
WHERE cat1 = :cat1 AND cat2 = :cat2
我不知道这是否只是这个特定 JPA 实现 (ObjectDB) 的问题。 不管怎样,如果有人有类似的问题,我希望这个post能帮到你。