问题
I'm working on creating a JPA 2.0 Annotation compliancy kit for my internship. Right now, I'm wondering when a @MapKeyTemporal annotation is required and when it's optional...
I know that when you define the column of the map key using @MapKeyColumn, the type the key should be mapped to can be derived by looking at the type of the column (and otherwise the type in the columndefinition). Thus, in this case, no @MapKeyTemporal annotation is necessary.
When you attach the @MapKeyTemporal annotation, the column name is defaulted to ATTRIBUTE + "_KEY".
When you don't annotate @MapKeyColumn and @MapKeyTemporal, the column name is defaulted to ATTRIBUTE + "_KEY", but to what type does the key default? Or are you supposed to get an error?
I looked for a similar situation and found @MapKeyEnumerated.
It's the same because it's related to @MapKeyColumn and it's a value that can be mapped to multiple datatypes (java.sql.Date/java.sql.Time/java.sql.Timestamp
for @MapKeyTemporal, and EnumeratedType.ORDINAL
/EnumeratedType.STRING
for @MapKeyEnumerated).
I found one difference:
@MapKeyEnumerated has a default. This default is EnumeratedType.ORDINAL
.
My question: When using a map that has a map key whose basic type is a temporal type, what's the default TemporalType (according to JPA 2.0) to which the map key is converted for persistence?
回答1:
Answer seems to be, that there is no default type when java.util.Date or java.util.Calendar is used as key of the map. Specification itself (I do consider javadocs delivered with specification as part of specification) is very strict about usage of MapKeyTemporal. Javadoc for MapKeyTemporal claims:
This annotation must be specified for persistent map keys of type Date and Calendar.
I think with such a strictness they forgot case that you present, having type information from attribute referenced by MapKey. It does not make too much sense to specify type in the case of MapKey, because type is already specified in the entity that is value of the map. Also allowing possibility to have different temporal type for key of the map for corresponding field in the entity is not desirable.
If MapKeyTemporal is not specified and if there is no other way to figure out type of the key, EclipseLink (reference implementation, though it is not always following specification) produces following exception:
org.eclipse.persistence.exceptions.ValidationException
Exception Description: The attribute [map] from the entity class [class X]
does not specify a temporal type. A temporal type must be specified for persistent
fields or properties of type java.util.Date and java.util.Calendar.
at org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:126)
回答2:
Basically, you are not asking if there is a default type for MapKeyTemporal but if implementations of JPA 2.0 or the spec are using a default value for Temporal Types.
Which implies that there is a more important type in a database between Date and Timestamp, that is probably not the case.
Also if you have a Calendar, the implementation probably calls getTime() and then constructs a new Sql type from there, how does it decide on the implementation ?
Reminder : The first reason for the existence of temporal is simply because a key with the class java.util.Date can be either of the three time types in Java (Time, Timestamp and sql.Date), while this doesn't matter in Java, it does in a database where these are distinct types.
If I do not specify a temporal type, nothing stops me from having different sql types for the same Entity, breaking at runtime.
来源:https://stackoverflow.com/questions/8971643/whats-the-default-temporaltype-for-a-temporal-map-key-without-a-mapkeycolumn-o