Hibernate CollectionOfElements EAGER fetch duplicates elements

后端 未结 6 1887
礼貌的吻别
礼貌的吻别 2020-12-04 11:28

I have a class called SynonymMapping which has a collection of values mapped as a CollectionOfElements

@Entity(name = \"synonymmapping\")
public class Synony         


        
6条回答
  •  北海茫月
    2020-12-04 11:47

    It's generally not a good idea to enforce eager fetching in the mapping - it's better to specify eager joins in appropriate queries (unless you're 100% sure that under any and all circumstances your object won't make sense / be valid without that collection being populated).

    The reason you're getting duplicates is because Hibernate internally joins your root and collection tables. Note that they really are duplicates, e.g. for 2 SynonymMappings with 3 collection elements each you would get 6 results (2x3), 3 copies of each SynonymMapping entity. So the easiest workaround is to wrap results in a Set thereby ensuring they're unique.

提交回复
热议问题