views:

38

answers:

3

Does JPA 1.0 support mapping of GregorianCalendar? I didn't find anything in JPA 1.0's mapping file specification about GregorianCalendar. What about JPA 2.0?

A: 

You can map java.util.Calendar with no special mapping.

It will give you the same result.

Vitaly Polonetsky
+1  A: 

JPA allows the mapping of java.util.Calendar (and its subclass). From the JPA 1.0 specification:

9.1.18 Basic Annotation

The Basic annotation is the simplest type of mapping to a database column. The Basic annotation can be applied to a persistent property or instance variable of any of the following types: Java primitive types, wrappers of the primitive types, java.lang.String, java.math.BigInteger, java.math.BigDecimal, java.util.Date, java.util.Calendar, java.sql.Date, java.sql.Time, java.sql.Timestamp, byte[], Byte[], char[], Character[], enums, and any other type that implements Serializable. As described in Section 2.1.6, the use of the Basic annotation is optional for persistent fields and properties of these types.

Pascal Thivent
+1  A: 

JPA does support java.util.Calendar and its subclasses. The only caveat is that you must use the @Temporal annotation to indicate how the field is stored in the database. Both versions of the spec have this requirement here's the section from the JPA 2.0 spec :

11.1.47 Temporal Annotation

The Temporal annotation must be specified for persistent fields or properties of type java.util.Date and java.util.Calendar. It may only be specified for fields or properties of these types.

The Temporal annotation may be used in conjunction with the Basic annotation, the Id annotation, or the ElementCollection[111] annotation (when the element collection value is of such a temporal type).

The TemporalType enum defines the mapping for these temporal types.

public enum TemporalType {
DATE, //java.sql.Date
TIME, //java.sql.Time
TIMESTAMP //java.sql.Timestamp
}

Otherwise there's nothing special you need to do. Your entity might look something like this :

@Entity 
public class Person { 
    // . . .

    @Temporal(TemporalType.TIMESTAMP)
    private GregorianCalendar lastUpdated;

    // . . .
}
Mike