![](/img/trans.png)
[英]problem in connecting to oracle 10g express edition through java
[英]Is there a Problem with JPA Entities, Oracle 10g and Calendar Type properties?
将 JPA 实体与 Oracle 10g 结合使用时,我遇到了以下非常烦人的行为。
假设您有以下实体。
@Entity
@Table(name = "T_Order")
public class TOrder implements Serializable {
private static final long serialVersionUID = 2235742302377173533L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(name = "activationDate")
private Calendar activationDate;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Calendar getActivationDate() {
return activationDate;
}
public void setActivationDate(Calendar activationDate) {
this.activationDate = activationDate;
}
}
该实体映射到 Oracle 10g,因此在 DB 中将有一个表T_ORDER
,其主键为NUMBER
列ID
和一个TIMESTAMP
列activationDate
。
假设我创建了一个此类的实例,激活日期为15. Sep 2008 00:00AM
。 我当地的时区是 CEST 是GMT+02:00
。 当我持久化这个对象并使用 sqlplus 从表T_ORDER
选择数据时,我发现表中实际上存储了14. Sep 2008 22:00
,到目前为止还可以,因为 oracle db 时区是 GMT。
但现在是烦人的部分。 当我将这个实体读回我的 JAVA 程序时,我发现 oracle 时区被忽略,我得到14. Sep 2008 22:00 CEST
,这绝对是错误的。
所以基本上,当写入数据库时,将使用时区信息,读取时将被忽略。
有什么解决办法吗? 我猜最简单的解决方案是将 oracle dbs 时区设置为GMT+02
,但不幸的是我不能这样做,因为还有其他应用程序使用同一台服务器。
我们使用以下技术
MyEclipse 6.5 JPA 与 Hibernate 3.2 Oracle 10g 瘦 JDBC 驱动程序
出于这个确切原因,您不应该使用日历来访问数据库中的日期。 你应该像这样使用java.util.Date
:
@Temporal(TemporalType.TIMESTAMP)
@Column(name="activationDate")
public Date getActivationDate() {
return this.activationDate;
}
java.util.Date
指向某个时刻,与任何时区无关。 日历可用于格式化特定时区或语言环境的日期。
我已经遇到了 JPA 和时间戳方面的问题。 我一直在oracle 论坛上阅读,请检查以下内容:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.