Pregunta

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.

This is the full query:

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)))

And the sepperate ones:

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

The full expression returns the same result as expression 2).

¿Fue útil?

Solución

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)))

Why this works: if do not explicitly left join, I suppose it makes an INNER JOIN which automatically will limit the results.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top