JPA实体,Oracle 10g和日历类型属性是否存在问题? [英] Is there a Problem with JPA Entities, Oracle 10g and Calendar Type properties?

查看:135
本文介绍了JPA实体,Oracle 10g和日历类型属性是否存在问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将JPA实体与Oracle 10g结合使用时,我遇到了以下非常烦人的行为。



假设您有以下实体。

  @Entity 
@Table(name =T_Order)
公共类TOrder实现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;
}

公共日历getActivationDate(){
return activationDate;
}

public void setActivationDate(Calendar activationDate){
this.activationDate = activationDate;
}
}

此实体映射到Oracle 10g,所以在DB将有一个表T_ORDER,主键NUMBER列为ID,TIMESTAMP列为activationDate。



假设我创建了这个类的实例激活日期15. 2008年9月00:00 AM。我当地的时区是CEST,格林尼治标准时间+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 with Hibernate 3.2
Oracle 10g瘦JDBC驱动程序

解决方案

出于这个原因,您不应该使用日历来访问数据库中的日期。你应该使用 java.util.Date

  @Temporal (TemporalType.TIMESTAMP)
@Column(name =activationDate)
public Date getActivationDate(){
return this.activationDate;
}

java.util.Date 指向某个时刻,不管任何时区。日历可用于格式化特定时区或区域设置的日期。


I'm experiencing the following very annoying behaviour when using JPA entitys in conjunction with Oracle 10g.

Suppose you have the following entity.

@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;
    }
}

This entity is mapped to Oracle 10g, so in the DB there will be a table T_ORDER with a primary key NUMBER column "ID" and a TIMESTAMP column "activationDate".

Lets suppose I create an instance of this class with the activation date "15. Sep 2008 00:00AM". My local timezone is CEST which is GMT+02:00. When I persist this object and select the data from the table T_ORDER using sqlplus, I find out that in the table actually "14. Sep 2008 22:00" is stored, which is ok so far, because the oracle db timezone is GMT.

But now the annoying part. When I read this entity back into my JAVA program, I find out that the oracle time zone is ignored and I get "14. Sep 2008 22:00 CEST", which is definitly wrong.

So basically, when writing to the DB the timezone information will be used, when reading it will be ignored.

Is there any solution for this out there? The most simple solution I guess would be to set the oracle dbs timezone to GMT+02, but unfortunatly I can't do this because there are other applications using the same server.

We use the following technology

MyEclipse 6.5 JPA with Hibernate 3.2 Oracle 10g thin JDBC Driver

解决方案

You should not use a Calendar for accessing dates from the database, for this exact reason. You should use java.util.Date as so:

@Temporal(TemporalType.TIMESTAMP)
@Column(name="activationDate")
public Date getActivationDate() {
    return this.activationDate;
}

java.util.Date points to a moment in time, irrespective of any timezones. Calendar can be used to format a date for a particular timezone or locale.

这篇关于JPA实体,Oracle 10g和日历类型属性是否存在问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆