1

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.

Community
  • 1
  • 1
Petr Kozelka
  • 7,670
  • 2
  • 29
  • 44

3 Answers3

1

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

Community
  • 1
  • 1
Christofer Dutz
  • 2,305
  • 1
  • 23
  • 34
  • Argh.. Sorry for that posting .. you said with xml configuration. So I guess simple EclipseLink is out of the question. But having a look at the hibernate documentation tells me it should be possible too ... http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/collections.html#collections-ofvalues – Christofer Dutz Aug 09 '11 at 14:58
1

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>
Petr Kozelka
  • 7,670
  • 2
  • 29
  • 44
0

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.

BishopRook
  • 1,240
  • 6
  • 11
  • thanks... ideally, I wish the solution is independent of implementation - otherwise I could just use Hibernate mapping which supports this easily... Unfortunately, your link shows an example with entity type value. Still, I would like to see _how_ painful it is; perhaps it makes me decide that this direction is wrong, and creating intermediate entity wrappers is easier. According to [this answer](http://stackoverflow.com/questions/853076/jpa-mapstring-string-mapping/888854#888854), it is perhaps possible with annotations; so I assumed that xml syntax allows it, too. – Petr Kozelka Aug 08 '11 at 20:39