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.

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

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 ... docs.jboss.org/hibernate/core/3.6/reference/en-US/html/…
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>

Comments

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.

1 Comment

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, it is perhaps possible with annotations; so I assumed that xml syntax allows it, too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.