2

我正在尝试将一个简单的属性映射作为我的一个持久对象的键值对持久化。我在这里找到了关于这个问题的一个很好的指南。但它只显示了当键的值是映射对象(具有“映射者”属性)时如何映射。我的场景比这更简单。我所需要的只是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]
4

1 回答 1

0

尝试为地图的值创建一个类,例如 TargetClass。那么地图将是这样的:

private Map<String, TargetClass> attributes;

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

于 2014-04-07T22:19:09.347 回答