Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA and GregorianCalendar

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?

like image 204
Candy Chiu Avatar asked Jul 19 '10 18:07

Candy Chiu


People also ask

Does JPA support instant?

JPA supports LocalDate , LocalTime , LocalDateTime etc. but not Instant.

What is the difference between Calendar and Gregorian calendar in Java?

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();

What is GregorianCalendar in Java?

GregorianCalendar is a concrete subclass of Calendar and provides the standard calendar system used by most of the world.

What is @temporal in JPA?

@Temporal is a JPA annotation that converts back and forth between timestamp and java. util. Date . It also converts time-stamp into time.


2 Answers

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;

    // . . .
}
like image 158
Mike Avatar answered Oct 04 '22 00:10

Mike


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.

like image 36
Pascal Thivent Avatar answered Oct 04 '22 00:10

Pascal Thivent