users@glassfish.java.net

JPA 2.0 Persistent Map Problem

From: <glassfish_at_javadesktop.org>
Date: Sun, 18 Jul 2010 10:07:13 PDT

I'm trying to build an entity with a persistent map. The map key is a basic type and the map value is an embeddable, like this:

    @ElementCollection(fetch=FetchType.EAGER)
    @MapKey(name = "language")
    @CollectionTable(schema = "jpa", name = "multilingual_string_map",
                     joinColumns = @JoinColumn(name = "string_id"))
    private Map<String, LocalizedString> map = new HashMap<String, LocalizedString>();

LocalizedString is a simple POJO:

@Embeddable
public class LocalizedString {
    private String language;
    private String text;
}

The language field of the map value is to be used as the map key. I absolutely want to avoid duplicating this in a redundant database table column. This is precisely what @MapKey is intended for, if I understand JSR 317 correctly, though there is a bit of vagueness whether or not the map value is allowed to be an embeddable or has to be an entity.

At any rate, none of the 4 JPA providers I've tried is able to handle this example. With Hibernate and Eclipselink, the closest I could get was using

@MapKeyColumn(name = "language_key")

instead, but this leaves me with the redundant table column "language_key" with the same values as in "language".

With OpenJPA, I could resort to using

@MapKeyColumn(name = "language")

which makes the two columns coincide, but I'm not sure if this usage is intended by the spec. Hibernate and Eclipselink complain about a duplicate column mapping in this case.

So who's right and who's wrong....?

Sorry for this messy layout, this forum engine does not seem to support embedded HTML. I posted a longer version of this example together with details regarding the individual JPA providers in my blog:

http://hwellmann.blogspot.com/2010/07/jpa-20-mapping-map.html

Best regards,
Harald
[Message sent by forum member 'hwellmann']

http://forums.java.net/jive/thread.jspa?messageID=477901