@ElementCollection的替代方法,使用JPA 1.0来持久化Map <string,string>?</string,string>

时间:2014-04-07 22:06:08

标签: java hibernate jpa orm jpa-1.0

我试图将一个简单的属性Map保存为我的一个持久对象的键值对。我在这个问题上找到了一个很好的指南here。但它仅显示了当键的值是映射对象时的映射方式(使用&#39;映射的&#39;属性)。我的场景比这简单。我所需要的只是坚持Map<String, String>一个键值对。我发现我可以使用@ElementCollection,但它仅在JPA 2上支持(我正在处理的平台仍在JPA1上)。

这是我的代码到目前为止的样子:

@Entity
public class MyClass {

    private Map<String, String> attributes;

    @OneToMany
    @MapKey(name="key")
    @JoinTable(name="MyClass_attributes")
    public Map<String, String> getAttributes () {
        return this.attributes;
    }

    public void setAttributes(Map<String, String> attributes) {
        this.attributes = attributes;
    }

}

但是我收到了这个错误:

Caused by: org.hibernate.AnnotationException: 
Use of @OneToMany or @ManyToMany targeting 
an unmapped class: mypackage.MyClass.attributes[java.lang.String]

1 个答案:

答案 0 :(得分:0)

尝试为地图的值创建一个类,如TargetClass。然后地图就是这样:

private Map<String, TargetClass> attributes;

在这种情况下,JoinTable并不需要。

相关问题