java JPA 和公历
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3283871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPA and GregorianCalendar
提问by Candy Chiu
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 1.0 是否支持映射GregorianCalendar?我在 JPA 1.0 的映射文件规范中没有找到任何关于GregorianCalendar. JPA 2.0 怎么样?
回答by Mike
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 :
JPA 确实支持 java.util.Calendar 及其子类。唯一需要注意的是,您必须使用 @Temporal 注释来指示字段在数据库中的存储方式。规范的两个版本都有这个要求,这里是 JPA 2.0 规范的部分:
11.1.47 Temporal Annotation
11.1.47 时间标注
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.
必须为 java.util.Date 和 java.util.Calendar 类型的持久字段或属性指定 Temporal 注释。只能为这些类型的字段或属性指定它。
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).
Temporal 注解可以与 Basic 注解、Id 注解或 ElementCollection[111] 注解结合使用(当元素集合值是这样的时间类型时)。
The TemporalType enum defines the mapping for these temporal types.
TemporalType 枚举定义了这些时间类型的映射。
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;
// . . .
}
回答by Pascal Thivent
JPA allows the mapping of java.util.Calendar(and its subclass). From the JPA 1.0 specification:
JPA 允许映射java.util.Calendar(及其子类)。来自 JPA 1.0 规范:
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.
9.1.18 基本注解
Basic 注释是到数据库列的最简单的映射类型。基本注释可以应用于以下任何类型的持久性或实例变量:Java基本类型,基本类型的包装,
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[],枚举,以及任何其他类型的器具Serializable. 如第 2.1.6 节所述,对于这些类型的持久字段和属性,基本注释的使用是可选的。
回答by Archimedes Trajano
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.
通常,您必须注意在 JPA 或 JDBC 中使用日历或日期,因为它们通常存储时没有时区信息。因此,您的应用程序很容易受到夏令时和时区差异的影响。
What I do on JPA is the following:
我在 JPA 上所做的如下:
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)
我使用 Date 或您喜欢的任何 Java 时间对象创建了一个 getter 和 setter(我更喜欢 Date 的不变性),但实际的列是 long 或 Long (不幸的是,这意味着需要进行空检查)
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.
这为您的实体用户提供了 Java Date 的灵活性,而不必担心时区和夏令时。
回答by m_vitaly
You can map java.util.Calendar with no special mapping.
您可以在没有特殊映射的情况下映射 java.util.Calendar。
It will give you the same result.
它会给你同样的结果。

