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?
Type used to indicate a specific mapping of java. util. Date or java. util. Calendar .
@MapKeyColumn is used to map the key to a property of target entity and this key is used to save as well as fetching records.
@Temporal is a JPA annotation that converts back and forth between timestamp and java. util. Date . It also converts time-stamp into time. For example, in the snippet below, @Temporal(TemporalType.
In JPA, @Temporal annotation solves the one of the major issue of converting the date and time values from Java object to compatible database type and retrieving back to the application.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With