Reputation: 6043
I'm working on a JPA 2.0 compliancy kit for my internship... Part of that kit is testing corner cases.
JSR-317 states on Page 360 that "The AttributeOverride
annotation may be applied to an element collection containing instances of an embeddable class or to a map collection whose key and/or value is an embeddable class."
How then, do I, according to JPA 2.0, override the mapping of a map of basic types? I know I can use @MapKeyColumn to map the key of the map, and I'm sure there is some way to map the value side of the @CollectionTable as well...
But how would I go about overriding these?
Consider an @Embeddable with a map
@CollectionTable
@MapKeyColumn(name="differentname_KEY")
Map<Integer, String> testMap;
How would I go about overriding the key and the value? Do I use @AttributeOverride, or something else? (Or is it impossible?!)
I'm assuming here that such a map would be mapped with a @CollectionTable, so please correct me if I'm wrong. If JPA doesn't give an answer, I'd be interested in knowing how persistence providers have solved this problem.
EDIT: Viruzzo commented that basic types are embeddable types. I'm willing to accept that, but something is holding me back: JSR-317 is referring to an embeddable class (see upper quote). Type and class are not the same...
Upvotes: 2
Views: 1093
Reputation: 42114
First as a side note: Map in your example should not even compile. Reason is that int is primitive type, java.util.Collection
collection and map interfaces and implementations are only for references types.
Lets use instead following example:
SomeEntity {
@Id private int id;
@ElementCollection
private Map<Integer, String> testmap;
}
By default testMap is mapped to the table SomeEntity_TESTMAP(SOMEENTITY_ID, TESTMAP, TESTMAPKEY)
. We have defaulted table name and three defaulted
column names. It is possible to override all of these. Following will map to the table testmap_table(join_column, value_column, key_column):
@ElementCollection
@CollectionTable(name = "testmap_table",
joinColumns = @JoinColumn(name = "join_column"))
@MapKeyColumn(name = "key_column")
@Column(name= "value_column")
private Map<Integer, String> testMap;
@AttributeOverride have no use here because no key or value is embeddable. It is for overriding mappings derived from elsewhere, not for overriding defaults of ElementCollection. It does have two usages:
Upvotes: 1