113

I am wondering if it is possible using annotations to persist the attributes map in the following class using JPA2

public class Example {
    long id;
    // ....
    Map<String, String> attributes = new HashMap<String, String>();
    // ....
}

As we already have a pre existing production database, so ideally the values of attributes could map to the following existing table:

create table example_attributes {
    example_id bigint,
    name varchar(100),
    value varchar(100));
corydoras
  • 7,130
  • 12
  • 55
  • 58

2 Answers2

216

JPA 2.0 supports collections of primitives through the @ElementCollection annotation that you can use in conjunction with the support of java.util.Map collections. Something like this should work:

@Entity
public class Example {
    @Id long id;
    // ....
    @ElementCollection
    @MapKeyColumn(name="name")
    @Column(name="value")
    @CollectionTable(name="example_attributes", joinColumns=@JoinColumn(name="example_id"))
    Map<String, String> attributes = new HashMap<String, String>(); // maps from attribute name to value

}

See also (in the JPA 2.0 specification)

  • 2.6 - Collections of Embeddable Classes and Basic Types
  • 2.7 Map Collections
  • 10.1.11 - ElementCollection Annotation
  • 11.1.29 MapKeyColumn Annotation
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • 1
    Is there a workaround to do this using JPA 1? I only found examples with `Map` – L. Holanda Apr 07 '14 at 23:06
  • 3
    Might be worth mentioning that `example_attributes` should have a composite key `(example_id, name)` - which is what hbm2ddl will generate from the above. – James Bassett Nov 20 '14 at 22:47
  • I tried above example, but when trying to persist an entity, I am getting an exception: You must define at least one mapping for this table. Query: InsertObjectQuery(null). Any hints? I create an empty entity and set properties map, then trying to persist. – Kamila Jul 14 '16 at 06:43
  • 3
    Using MariaDB, I ran into `Specified key was too long; max key length is 767 bytes` doing this. The primary key it tries to create is a combination of the varchar(255) and the @JoinColumn, which exceeds the default column size. You need to either [change your database](http://stackoverflow.com/questions/1814532/1071-specified-key-was-too-long-max-key-length-is-767-bytes) or modify your @MapKeyColumn to provide a length: `@MapKeyColumn(name="name", length=100)` – Jon Sep 09 '16 at 18:48
  • This worked for me! Had to make sure that I my table was referencing my other table's PK. – mr nooby noob Sep 13 '21 at 12:40
23
  @ElementCollection(fetch = FetchType.LAZY)
  @CollectionTable(name = "raw_events_custom", joinColumns = @JoinColumn(name =     "raw_event_id"))
  @MapKeyColumn(name = "field_key", length = 50)
  @Column(name = "field_val", length = 100)
  @BatchSize(size = 20)
  private Map<String, String> customValues = new HashMap<String, String>();

This is an example on how to set up a map with control over column and table names and field length.

wciesiel
  • 558
  • 5
  • 14