JPA/Hibernate duplicate records

前端 未结 1 725
误落风尘
误落风尘 2021-01-16 08:45

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

相关标签:
1条回答
  • 2021-01-16 09:09

    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...
    
    0 讨论(0)
提交回复
热议问题