2

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...

7
  • 2
    Basic types are embeddable; I'd say it's just a poor choice of words.
    – Viruzzo
    Jan 27, 2012 at 11:48
  • If you added that as an answer I could accept it... Are you sure there's not a difference? There's a difference in depth from what I've seen: in case of an embeddable it would be @AttributeOverride(name="embeddable.testMap.key.attribute" [column omitted]) and in case of a basic type it would then be @AttributeOverride(name="embeddable.testMap.key" [column omitted]).
    – Pimgd
    Jan 27, 2012 at 12:03
  • @MapKeyClass also states that "The map key can be a basic type, an embeddable class, or an entity.", leading to further support that an embeddable type is not the same as an embeddable class.
    – Pimgd
    Feb 8, 2012 at 14:16
  • I didn't say they are the same, just that basic types are embeddable (not an embeddable class); simply put, an embeddable class is one that is incorporated inside the container class when passed to JPA, which is the native (and only) behaviour for basic types.
    – Viruzzo
    Feb 9, 2012 at 9:12
  • Okay, ... ... how does that answer the question? I'm having trouble understanding it.
    – Pimgd
    Feb 10, 2012 at 13:21

1 Answer 1

1

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:

  • overriding attribute mappings derived from mapped superclass or derived from the embedded class
  • overriding attribute mappings derived from the embeddable class that is used as key or value in element collection.
4
  • fixed the compilation error, thanks. I'll look at this in more detail when I arrive at work tomorrow... I see that @AttributeOverride can't be used here - what would be used?
    – Pimgd
    Feb 27, 2012 at 0:16
  • Used for what, can you give example about functionality you miss? In example I gave there is three columns and for all of them default name of the column is overridden (@JoinColumn, MapKeyColumn, @Column). Feb 27, 2012 at 5:34
  • How would the mapping be overridden outside of the class of SomeEntity (like how @AttributeOverride works - overriding a mapping in class A from class B)
    – Pimgd
    Feb 27, 2012 at 10:36
  • Most likely there is no way to do that. Mar 8, 2012 at 21:17

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.