简体   繁体   English

如何 map 集合 Map <string,list<string> &gt; 使用 Hibernate 键入</string,list<string>

[英]How to map a collection Map<String,List<String>> type using Hibernate

The question was to persist the following class using Hibernate.问题是使用 Hibernate 坚持以下 class。

Public class Album{
Private int albumid;
Private String aname;
Private Map<String,List<String>> photos;
}

I have Tried this我试过这个

@Entity
public class Album {
    @Id
    private int albumId;
    @Column(name= "Album_Name")
    private String aname;
    
    @ElementCollection
    @MapKeyColumn(name= "Event_Name")
    @Column(name="Values")
    private Map<String, List<String>> photos;

But it is showing errors such as但它显示错误,例如


    Exception in thread "main" org.hibernate.MappingException: Could not determine type for: java.util.List, at table: Album_photos, for columns: [org.hibernate.mapping.Column(Values)]
        at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:336)
        at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:310)
        at org.hibernate.mapping.Collection.validate(Collection.java:315)
        at org.hibernate.mapping.IndexedCollection.validate(IndexedCollection.java:89)
        at org.hibernate.cfg.Configuration.validate(Configuration.java:1362)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1849)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
        at com.wipro.Insert.main(Insert.java:17)

This mapping is not going to work in Hibernate ORM.此映射不适用于 Hibernate ORM。

The reason is that you are trying to have two nested collections of elements and this is not supported by Hibernate ORM (first collection is the Map and the second collection is the the List ). The reason is that you are trying to have two nested collections of elements and this is not supported by Hibernate ORM (first collection is the Map and the second collection is the the List ).

You have to use entities.你必须使用实体。

You can obtain something similar to an @ElementCollection with the following mapping:您可以通过以下映射获得类似于@ElementCollection的内容:


@Entity
public static class PhotoEvent {
    @Id
    @Column(name = "Event_Name")
    public String eventName;

    @ElementCollection
    @Column(name = "`Values`")
    public List<String> values;

    @ManyToOne
    public Album album;
...
// getter/setter/hascode/equals...
}


@Entity
public static class Album {
    ...
    
    @OneToMany(mappedBy = "album", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
    @MapKey(name = "eventName")
    public Map<String, PhotoEvent> photoEvents;
    
    ...
}

Note that I set FetchType.EAGER because it emulates an @ElementCollection but you will probably want to set it to LAZY (the default).请注意,我设置FetchType.EAGER是因为它模拟了@ElementCollection ,但您可能希望将其设置为LAZY (默认值)。

You will find more details about this type of mapping in the Hibernate ORM documentation .您将在 Hibernate ORM 文档中找到有关此类映射的更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM