简体   繁体   English

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

[英]Alternative to @ElementCollection using JPA 1.0 for persisting Map<String,String>?

I'm trying to persist a simple Map of attributes as a key value pair for one of my persisted objects. 我试图将一个简单的属性映射作为一个持久对象的键值对持久化。 I found a good guide on this matter here . 我在这里找到了关于这个问题的很好的指南。 But it only shows how to map when the value of the key is a mapped object (with a 'mapped by' property). 但是,它仅显示了键的值为映射对象(具有“ mapped by”属性)时如何进行映射。 My scenario is simpler than this. 我的情况比这简单。 All I need is to persist a Map<String, String> for a key value pair. 我所需要做的就是为键值对保留Map<String, String> I found that I could use @ElementCollection but it is only supported on JPA 2 (the platform I'm working on is still on JPA1). 我发现我可以使用@ElementCollection但是它仅在JPA 2上受支持(我正在使用的平台仍在JPA1上)。

This is how my code looks like so far: 到目前为止,我的代码是这样的:

@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;
    }

}

But I'm getting this error: 但我收到此错误:

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

Try to create a class for the map's value, like TargetClass. 尝试为地图的值创建一个类,例如TargetClass。 Then the map would be this: 那么地图将是这样的:

private Map<String, TargetClass> attributes;

In this case the JoinTable doesn't need. 在这种情况下,不需要JoinTable。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM