2

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]
L. Holanda
  • 4,432
  • 1
  • 36
  • 44

1 Answers1

0

Try to create a class for the map's value, like TargetClass. Then the map would be this:

private Map<String, TargetClass> attributes;

In this case the JoinTable doesn't need.

znurgl
  • 1,077
  • 7
  • 12
  • That's what I was trying to avoid. That seems to much overhead for something really simple. It would be great if I could do something like this: http://stackoverflow.com/questions/3393649/storing-a-mapstring-string-using-jpa but I need a JPA 1 solution. – L. Holanda Apr 07 '14 at 22:21
  • Well using JPA is basically dealing with overhead stuffs. – znurgl Apr 08 '14 at 14:24
  • 1
    Well... since we're using Hibernate, I'll simply use `@CollectionOfElements` (org.hibernate.annotations.CollectionOfElements) – L. Holanda Apr 08 '14 at 19:01