Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing a Map<String,String> using JPA

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)); 
like image 647
corydoras Avatar asked Aug 03 '10 04:08

corydoras


People also ask

How does JPA return map key values?

There is no standard way to get JPA to return a map. Iterating manually should be fine. The time to iterate a list/map in memory is going to be small relative to the time to execute/return the query results.

What is mapping in JPA?

The One-To-One mapping represents a single-valued association where an instance of one entity is associated with an instance of another entity. In this type of association one instance of source entity can be mapped atmost one instance of target entity.

How do you store a value on a map?

a) The values can be stored in a map by forming a key-value pair. The value can be retrieved using the key by passing it to the correct method. b) If no element exists in the Map, it will throw a 'NoSuchElementException'. c) HashMap stores only object references.

Can JPA return results as a map?

If a given JPA GROUP BY query returns only two columns where one is unique, it's very suitable to return the result as a Java Map. For this, you can use either the Java Stream functionality or the Hibernate-specific ResultTransformer .


2 Answers

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
like image 176
Pascal Thivent Avatar answered Oct 10 '22 10:10

Pascal Thivent


  @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.

like image 45
wciesiel Avatar answered Oct 10 '22 11:10

wciesiel