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). My scenario is simpler than this. All I need is to persist a Map<String, String>
for a key value pair. 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).
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. Then the map would be this:
In this case the JoinTable doesn't need.