JPA and GregorianCalendar
Solution 1
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;
// . . .
}
Solution 2
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 implementsSerializable
. As described in Section 2.1.6, the use of the Basic annotation is optional for persistent fields and properties of these types.
Solution 3
You have to watch out with using calendars or dates with JPA or JDBC in general because they are usually stored with no timezone information. As such your application would be susceptible to daylight savings time and timezone differences.
What I do on JPA is the following:
I create a getters and setters using Date or whatever Java temporal objects you are comfortable with (I prefer Date for its immutability) but the actual column is a long or Long (unfortunately it implies a null check needs to be done)
private Long timestamp;
public void setTimestamp(final Date timestamp) {
if (timestamp == null) {
this.timestamp = null;
} else {
this.timestamp = timestamp.getTime();
}
}
public Date getTimestamp() {
if (timestamp == null) {
return null;
}
return new Date(timestamp);
}
This gives your entity users the flexibility of Java Date without having to concern themselves of timezone and daylight savings.
Candy Chiu
Updated on June 06, 2022Comments
-
Candy Chiu almost 2 years
Does JPA 1.0 support mapping of
GregorianCalendar
? I didn't find anything in JPA 1.0's mapping file specification aboutGregorianCalendar
. What about JPA 2.0?