Java 如何将JPA映射为HashMap?
我应该如何注释一个Java 如何将JPA映射为HashMap?,java,jakarta-ee,jpa,Java,Jakarta Ee,Jpa,我应该如何注释一个HashMap,以便JPA能够持久化它 @Entity public class User { private Map<UserType, Boolean> settings; } public enum UserType { USER, ADMIN } @实体 公共类用户{ 私人地图设置; } 公共枚举用户类型{ 用户,管理员 } 我尝试了@manytomy(cascade=CascadeType.ALL)但这给了我一个错误,即布尔值不是实体
HashMap
,以便JPA能够持久化它
@Entity
public class User {
private Map<UserType, Boolean> settings;
}
public enum UserType {
USER, ADMIN
}
@实体
公共类用户{
私人地图设置;
}
公共枚举用户类型{
用户,管理员
}
我尝试了
@manytomy(cascade=CascadeType.ALL)
但这给了我一个错误,即布尔值
不是实体(当然是真的)。manytomy
不是HashMap
的最佳解决方案。使用@ElementCollection注释映射表,中间表将生成命名的user\u type
(如注释中所定义)。以下是注释示例:
@ElementCollection
@CollectionTable(name = "user_type")
@MapKeyColumn(name = "user_type_col")
@Column(name = "boolean_col")
private Map<UserType, Boolean> userType = new HashMap<>();
@ElementCollection
@CollectionTable(name=“用户类型”)
@MapKeyColumn(name=“user\u type\u col”)
@列(name=“boolean\u col”)
私有映射userType=newhashmap();