Duplicates in OneToMany annotated List

前端 未结 1 603
伪装坚强ぢ
伪装坚强ぢ 2021-01-06 04:33

I\'m working on a Java project using JPA 2 + Hibernate 4.2.6 and I\'m getting a strange behaviour.

In my model I have two related entites: Question and

相关标签:
1条回答
  • 2021-01-06 05:34

    You are most likely getting duplicates because when using fetch=FetchType.EAGER, Hibernate uses an outer join to fetch the data in the joined table.

    Try removing the eager fetching to confirm. If it is the case, you should either remove the eager fetching, keep a Set instead of a List, or write a JPQL query to retrieve exactly what you need.

    From Hibernate doc:

    The recommanded approach is to use LAZY on all static fetching definitions and override this choice dynamically through JP-QL.

    0 讨论(0)
提交回复
热议问题