Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 如何映射postgresql“;带时区的时间戳“;在JPA 2实体中_Hibernate_Postgresql_Jpa_Jpa 2.0 - Fatal编程技术网

Hibernate 如何映射postgresql“;带时区的时间戳“;在JPA 2实体中

Hibernate 如何映射postgresql“;带时区的时间戳“;在JPA 2实体中,hibernate,postgresql,jpa,jpa-2.0,Hibernate,Postgresql,Jpa,Jpa 2.0,我有一个JPA2应用程序(Hibernate 3.6作为JPA实现),它使用Postgresql(带有9.0-801.jdbc3 JDBC驱动程序) 我无法将“时区时间戳”字段映射到我的JPA实体 以下是一个例子: CREATE TABLE theme ( id serial NOT NULL, # Fields that are not material to the question have been edited out run_from timestamp with tim

我有一个JPA2应用程序(Hibernate 3.6作为JPA实现),它使用Postgresql(带有9.0-801.jdbc3 JDBC驱动程序)

我无法将“时区时间戳”字段映射到我的JPA实体

以下是一个例子:

CREATE TABLE theme
(
  id serial NOT NULL,
  # Fields that are not material to the question have been edited out
  run_from timestamp with time zone NOT NULL,
  run_to timestamp with time zone NOT NULL,
  CONSTRAINT theme_pkey PRIMARY KEY (id ),
  CONSTRAINT theme_name_key UNIQUE (name )
)
我已尝试绘制以下地图:

@Entity
@Table(schema = "content", name = "theme")
public class Theme extends AbstractBaseEntity {
    private static final long serialVersionUID = 1L;

    @Column(name = "run_from")
    @NotNull
    @Temporal(TemporalType.TIMESTAMP)
    private Date runFrom;

    @Column(name = "run_to")
    @NotNull
    @Temporal(TemporalType.TIMESTAMP)
    private Date runTo;

    /* The rest of the entity has been edited out */
我不断得到一个异常,其根本原因如下:
由以下原因引起:org.hibernate.HibernateException:public.backend\u主题中创建的列的列类型错误。找到:timestamptz,预期:日期

我尝试过的

  • java.util.Calendar
    替换为
    java.util.Date
    ——没有任何区别
  • 使用
    java.sql.Timestamp
    -抱怨我无法将
    @Temporal
    注释应用于
    时间戳
  • 使用带有自定义
    @Type
    注释(
    @Type(Type=“org.joda.time.contrib.hibernate.PersistentDateTimeTZ”)
    org.joda.time.DateTime
    也不起作用
约束

  • 此应用程序与“遗留系统”交互,因此,更改日期字段的类型不是一个好的选择
我的问题是:我应该如何将这些时区感知的时间戳映射到我的JPA实体中?

我最终通过关闭模式验证实现了这一“工作”——以一种骇人的方式

以前,我在persistence.xml中有
“hibernate.hbm2ddl.auto”
。当我注释掉这个属性时,我的应用程序服务器启动了,模型“工作”

我的实体的最终形式是:

@Entity
@Table(schema = "content", name = "theme")
public class Theme extends AbstractBaseEntity {
    private static final long serialVersionUID = 1L;

    @Column(name = "run_from", columnDefinition = "timestamp with time zone not null")
    @NotNull
    @Temporal(TemporalType.TIMESTAMP)
    private Date runFrom;

    @Column(name = "run_to", columnDefinition = "timestampt with time zone not null")
    @NotNull
    @Temporal(TemporalType.TIMESTAMP)
    private Date runTo;

    /* Getters, setters, .hashCode(), .equals() etc omitted */
在读了很多这方面的内容后,我得到的印象是,没有简单的方法可以将Postgresql时间戳映射为时区列

一些JPA实现+数据库组合本机支持这一点(EclipseLink+Oracle就是一个例子)。对于hibernate,使用jodatime扩展,可以使用时区的普通时间戳+varchar字段来存储时区感知的时间戳(我不能这样做,因为我被限制更改数据库模式)。或者完全自定义的用户类型也可以用来解决这个问题

我需要注意的是,我对这个实体的用例是“只读”的,所以我可以用一个看似幼稚的“解决方案”逃脱惩罚

添加
@Column(columnDefinition=“带时区的时间戳”)


这实际上仅对DDL自动或在运行时有用吗?
columnDefinition
值仅在DDL中使用。但是,如果您的表列是使用该DDL创建的,那么它也会间接影响运行时行为的细节。
@Column(name = "run_from", columnDefinition= "TIMESTAMP WITH TIME ZONE")
@NotNull
@Temporal(TemporalType.TIMESTAMP)
private Date runFrom;