Hibernate持久化Map< String,String>而无需引用其他表格 [英] Hibernate persist Map<String, String> without referencing another tables

查看:164
本文介绍了Hibernate持久化Map< String,String>而无需引用其他表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



映射值来自客户端并且是随机的,所以我不用''不想为地图值存储单独的表格



异常




: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&lt; String,String&gt;而无需引用其他表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆