Petr Kozelka
Petr Kozelka

Reputation: 7980

JPA Map<String,String> mapping with xml

I am trying to define xml mapping for a Map<String,String> field.

The entity class cannot be modified so I am using the XML variant of JPA mapping, but cannot figure out the proper syntax.

Can someone explain how to write the JPA xml for this case - or explicitly state that this is impossible with xml but possible with annotations as mentioned in Storing a Map<String,String> using JPA ...

I will even appreciate to know that this is impossible - ideally when it comes with reference to the part of specification that states it.

Upvotes: 1

Views: 1140

Answers (3)

Petr Kozelka
Petr Kozelka

Reputation: 7980

After more time and searching for different things I happened to find the answer here: http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/new_collection_mappings#XML_2

The solution is:

<element-collection name="quotes">
  <column name="QUOTE"/>
  <map-key-column name="Q_DATE"/>
  <collection-table name="EBC_QUOTES">
    <join-column name="EBC_ID"/>
  </collection-table>
</element-collection>

Upvotes: 1

Christofer Dutz
Christofer Dutz

Reputation: 2365

These primitive relations have been added in JPA2 so you have to use a JPA2 imeplementation. I use Eclipselink. The keyword is "ElementCollection". It seems this has allready been discussed here: Storing a Map<String,String> using JPA

Upvotes: 1

BishopRook
BishopRook

Reputation: 1260

You haven't specified which JPA implementation you're using, but I think this should work for both OpenJPA and Hibernate... See here:

http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Example_of_a_map_key_column_relationship_XML

The difficulty you'll run into is that you're mapping to a primitive type instead of an entity type. I won't say it's impossible, but I will say from experience that it's painful.

Upvotes: 0

Related Questions