I have a One-to-Many relationship between entities. When doing this JPQL query:
SELECT parent FROM Parent parent JOIN parent.child child WHERE ...
I get dupl
You can get rid of the duplicates by using the DISTINCT
keyword:
SELECT DISTINCT parent FROM Parent parent JOIN parent.child child WHERE ...
EDIT: The DISTINCT
keyword is used to remoe duplicates from query results regardless of teh reason for the existence of these duplicates. Sometimes the reason is duplicate DB entries. But more often, duplicates are the consequence of JOIN
statements, so your use case is completely legitimate.
However, you could avoid explicit joins and the DISTINCT
keyword by making the relation bidirectional. Then you can use implicit joins through navigation:
SELECT parent FROM Parent parent WHERE parent.children...