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 i开发者_JAVA技巧n 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.
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
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>
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.
精彩评论