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?
JPA supports LocalDate , LocalTime , LocalDateTime etc. but not Instant.
The major difference between GregorianCalendar and Calendar classes are that the Calendar Class being an abstract class cannot be instantiated. So an object of the Calendar Class is initialized as: Calendar cal = Calendar. getInstance();
GregorianCalendar is a concrete subclass of Calendar and provides the standard calendar system used by most of the world.
@Temporal is a JPA annotation that converts back and forth between timestamp and java. util. Date . It also converts time-stamp into time.
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;
// . . .
}
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.
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