1

I am a newbie at JPA and was not able to find a good google solution to this. I am wanting to set up a JPA annotation for a LinkedHashMap of strings. Here is the twist, I have a abstract class that I am mapping as a mappedSuperClass. So that every implementation of this superclass has the LinkedHashMap of strings. Here is my example:

@MappedSuperclass
public abstract class AbstractCustomerType implements Serializable
{

@ElementCollection  //is this right?
protected LinkedHashMap<String, String> customerData;
}

So what can I expect in my database? Each implementation of AbstractCustomerType having its own join table of customerData? In that case if I have 10 CustomerTypes I would have 10 customerData tables? How would I annotate this so that I only get one customerData table that is shared between all customerTypes?

4

2 回答 2

2

That won't work at all. Persistent collections must be typed with an interface type, and not with a concrete type: Map, Set or List, but not HashMap, LinkedHashMap, HashSet or ArrayList.

See page 23 of the specification:

Collection-valued persistent fields and properties must be defined in terms of one of the following collection-valued interfaces regardless of whether the entity class otherwise adheres to the JavaBeans method conventions noted above and whether field or property access is used: java.util.Collection, java.util.Set, java.util.List, java.util.Map.

于 2013-08-07T20:10:30.947 回答
0

Try using the @CollectionTable annotation to define a single table. Also ensure your ids are unique across subclasses (use the same sequence).

Also consider using JOINED inheritance.

JPA technically only allows interfaces, but EclipseLink will allow the implementation (if EAGER, or LAZY in the dev builds (2.6)).

于 2013-08-08T13:23:02.800 回答