Hibernate CollectionOfElements EAGER fetch duplicates elements

后端 未结 6 1889
礼貌的吻别
礼貌的吻别 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 12:01

    I stepped into the same problem - when you set the FetchType.EAGER for a @CollectionOfElements, the Hibernate tries to get everything in one shot, i.e. using one single query for each entry of element linked to a "master" object. This problem can be successfully solved at a cost of N+1 query, if you add the @Fetch (FetchMode.SELECT) annotation to your collection. In my case I wanted to have a MediaObject entity with a collection of its metadata items (video codec, audio codec, sizes, etc.). The mapping for a metadataItems collection looks as follows:

    
    @CollectionOfElements (targetElement = String.class, fetch = FetchType.EAGER)
    @JoinTable(name = "mo_metadata_item", joinColumns = @JoinColumn(name = "media_object_id"))
    @MapKey(columns = @Column(name = "name"))
    @Column (name = "value")
    @Fetch (FetchMode.SELECT)
    private Map metadataItems = new HashMap();
    

提交回复
热议问题