[英]How to map a collection Map<String,List<String>> type using Hibernate
問題是使用 Hibernate 堅持以下 class。
Public class Album{
Private int albumid;
Private String aname;
Private Map<String,List<String>> photos;
}
我試過這個
@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;
但它顯示錯誤,例如
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)
此映射不適用於 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
).
你必須使用實體。
您可以通過以下映射獲得類似於@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;
...
}
請注意,我設置FetchType.EAGER
是因為它模擬了@ElementCollection
,但您可能希望將其設置為LAZY
(默認值)。
您將在 Hibernate ORM 文檔中找到有關此類映射的更多詳細信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.