Hibernate持久化Map< String,String>而无需引用其他表格 [英] Hibernate persist Map<String, String> without referencing another tables
问题描述
映射值来自客户端并且是随机的,所以我不用''不想为地图值存储单独的表格
异常
:org.hibernate.AnnotationException:关联类不是
found:java.lang.String
代码
@Entity
public class UserConfig {
@Id
@SequenceGenerator(sequenceName =CONFIG_SEQ ,name =ConfigSeq,allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator =ConfigSeq)
private Long id;
$ b @ElementCollection(targetClass = String.class)
@CollectionTable(name =MAP)
@MapKey(name =key)
@Column( name =value)
私人地图< String,String>地图;
更新
您可以吗如果 MyEnum
是未映射的类,请解释如何保存 Map< MyEnum,String>
?
根据规范,您应该如下注释地图:
@ElementCollection(targetClass = String.class)
@CollectionTable(name =MAP)
@MapKeyColumn(name =key)
@Column(name =value)
私人地图< String,String>地图;
所以 @MapKeyColumn
,而不是 @MapKey
。
当你的地图定义为: $ b $时, b
私人地图<基本,基本>地图; //(即Map< String,String>)
使用 Map Map
注释当您将地图定义为:
private Map< Basic,Entity>地图; //(即Map< String,User>)
最后,使用 @MapKeyEnumerated
注释:
private Map< Enumeration,Basic>地图; //(即Map< MyEnum,String>)
Could you please help me to persist map of strings with Hibernate?
Map values come from client and are random, so I don't want to store separate table for map's values
Exception
Caused by: org.hibernate.AnnotationException: Associated class not found: java.lang.String
Code
@Entity
public class UserConfig {
@Id
@SequenceGenerator(sequenceName = "CONFIG_SEQ", name = "ConfigSeq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ConfigSeq")
private Long id;
@ElementCollection(targetClass = String.class)
@CollectionTable(name = "MAP")
@MapKey(name="key")
@Column(name="value")
private Map<String, String> map;
Update
Could you please also explain how to persist Map<MyEnum, String>
, if MyEnum
is an unmapped class?
According to the specification, you should annotate the map like this:
@ElementCollection(targetClass = String.class)
@CollectionTable(name = "MAP")
@MapKeyColumn(name="key")
@Column(name="value")
private Map<String, String> map;
So @MapKeyColumn
, instead of @MapKey
.
This is the way you should annotate the map when its defined as:
private Map<Basic, Basic> map; // (i.e. Map<String, String>)
You use the @MapKey
annotation when you have map defined as:
private Map<Basic, Entity> map; // (i.e. Map<String, User>)
And finally, you use @MapKeyEnumerated
annotation when you have map defined ad:
private Map<Enumeration, Basic> map; // (i.e. Map<MyEnum, String>)
这篇关于Hibernate持久化Map< String,String>而无需引用其他表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!