JPQL,还是仅返回一个条件的结果? [英] JPQL, OR only returning result of one condition?
问题描述
我正在尝试编写一个JPQL查询,该查询应该获得一个与至少两个条件之一匹配的列表.当我分别构造查询时,它们将按预期工作,但是将它们放在一起的"OR"将返回仅匹配条件之一的列表.我不明白为什么会这样.
I'm trying to write a JPQL query which should get a list which match atleast one of two conditions. When I construct the queries sepperatly they work as expected, but putting them together in an 'OR' returns a list which only match one of the conditions. I don't understand why this is.
这是完整的查询:
SELECT a FROM Article a WHERE ((a.ag.proteinPID.uniprot.AC LIKE :genProt)
OR (a.aid IN(SELECT a2.aid FROM Protein p JOIN p.articleList a2 WHERE p.uniprot.AC LIKE :genProt)))
还有其他的:
1)
SELECT a FROM Article a WHERE a.aid IN(SELECT a2.aid FROM Protein p JOIN p.articleList a2 WHERE p.uniprot.AC LIKE :genProt)
2)
SELECT a FROM Article a WHERE a.ag.proteinPID.uniprot.AC LIKE :genProt
完整表达式返回与表达式2)相同的结果.
The full expression returns the same result as expression 2).
推荐答案
在第一个条件下,尝试在完整查询中左移实体:
Try left joining the entities within the full query for the first condition:
SELECT a FROM Article a LEFT JOIN a.ag g LEFT JOIN g.proteinPID p LEFT JOIN p.uniport u WHERE ((u.AC LIKE :genProt)
OR (a.aid IN(SELECT a2.aid FROM Protein p JOIN p.articleList a2 WHERE p.uniprot.AC LIKE :genProt)))
为什么行得通:如果不显式退出联接,我想它会产生一个 INNER JOIN
,它会自动限制结果.
Why this works: if do not explicitly left join, I suppose it makes an INNER JOIN
which automatically will limit the results.
这篇关于JPQL,还是仅返回一个条件的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!