What is the default TemporalType for a temporary map key without annotation @MapKeyColumn or @MapKeyTemporal?

I am working on creating a JPA 2.0 compatibility set for my internship. Right now, I am wondering when the @MapKeyTemporal annotation is required and when it is optional ...

I know that when you define a column of a map key using @MapKeyColumn, the type to which the key should be bound can be obtained by looking at the type of the column (and otherwise, the type in the column definition). Thus, in this case, the @MapKeyTemporal annotation is not required.

When you attach the @MapKeyTemporal annotation, the default column name is ATTRIBUTE + "_KEY".

When you do not comment on @MapKeyColumn and @MapKeyTemporal, the default column name is ATTRIBUTE + "_KEY", but what is the default key type? Or should you get an error message?

I was looking for a similar situation and found @MapKeyEnumerated. This is the same with @MapKeyColumn, and it is a value that can be mapped to multiple data types (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 value. This default value is EnumeratedType.ORDINAL .

My question: When using a card with a card key whose base type is a temporary type, what is the default TemporalType (in accordance with JPA 2.0) to which the card key is converted for saving?

+7
source share
2 answers

The answer seems to be that there is no default type when using java.util.Date or java.util.Calendar as the map key. The specification itself (I believe javadocs comes with the specification as part of the specification) is very strict regarding the use of MapKeyTemporal. Javadoc for MapKeyTemporal claims:

This annotation must be specified for permanent keys of a map of type Date and Calendar.

I think that with such severity they forget what you represent, having information about the type from the attribute referenced by MapKey. It makes no sense to indicate a type in the case of MapKey, because the type is already specified in the object, which is the value of the map. In addition, reluctance to have a different temporary type for the card key for the corresponding field in the object is undesirable.

If MapKeyTemporal is not specified, and if there is no other way to determine the type of key, EclipseLink (the reference implementation, although not always the following specification) throws the 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) 
+3
source

Basically, you are not asking if there is a default type for MapKeyTemporal, but if JPA 2.0 or spec implementations use the default value for temporary types.

This means that there is a more important type in the database between Date and Timestamp, which is probably not the case.

Also, if you have a Calendar, the implementation probably calls getTime () and then creates a new Sql type, how does it decide on the implementation?

Reminder: The first reason for the existence of a temporary one is simply because a key with the java.util.Date class can be one of three types of time in Java (Time, Timestamp and sql.Date), while it does not matter in Java, it works in a database where these are different types.

If I don't specify a temporary type, nothing prevents me from having different sql types for the same Entity, breaking at runtime.

0
source

All Articles