我正在尝试存储Map<String, List<String>>
;使用JPA。
我的实体看起来像:
@Entity
@Table(name = "Profiles_table")
public class Profiles {
@Id
@Column(name = "profile_ID", updatable = false, nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private final HashMap<String, List<String>> AllProfiles;
...
}
我为地图尝试了很多设置,但它不起作用......
我试过的最后一个:
@ElementCollection
@MapKeyColumn(name = "Profil")
@Column(name = "Permissions")
@CollectionTable(name = "Profiles_permissions", joinColumns = @JoinColumn(name = "profile_ID"))
抛出以下异常:
org.hibernate.AnnotationException: Illegal attempt to map a non collection as a
@OneToMany, @ManyToMany or @CollectionOfElements: [...]Profiles.AllProfiles
提前致谢
答案 0 :(得分:1)
字符串不是实体,所以你不应该使用@OneToMany等...
你试过这个:
@CollectionOfElements
private Map<String, List<String>> allProfiles;
答案 1 :(得分:1)
不是真正的答案,但这是我做的方式。
我将第二级集合存储为blob。
@Entity
@Table(name = "Profiles_table")
public class Profiles {
@Id
@Column(name = "profile_ID", updatable = false, nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(length = 16777210)
private final HashMap<String, Set<String>> AllProfiles;
答案 2 :(得分:0)
您是否尝试将AllProfiles声明为接口类型(比方说Map)? 查看"Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements" in hibernate when annotating a ConcurrentHashMap