【问题标题】:postgresql jpa hibernate exception: Found: date, expected: timestamppostgresql jpa休眠异常:发现:日期,预期:时间戳
【发布时间】:2016-06-12 07:21:03
【问题描述】:

在应用程序部署期间,JPA @Temporal(TemporalType.DATE) 注释存在一些问题。 所以我创建了 spring data + postgres 9.5 + hibernate 4.3.10 应用程序并将 hbm2ddl.auto 设置为 'validate'。

hibernate.hbm2ddl.auto = validate

这里是实体:

@Entity
@Table(name = "locomotive")
public class LocomotiveEntity {

    private Long locomotiveId;
    @Column(name = "status")
    private int status;
    @Temporal(TemporalType.DATE)
    private java.util.Date constructionYear;
...

我还使用此脚本创建了 locomotive 表:

CREATE TABLE "locomotive"
(
    "locomotiveId"  bigserial PRIMARY KEY,
    "status" integer NOT NULL,
    "constructionYear" DATE NOT NULL
);

但我在部署期间看到异常:

Caused by: org.hibernate.HibernateException: Wrong 
column type in public.constructionYear for column constructionYear.
 Found: date, expected: timestamp

所以我的数据库中有“DATE”类型列,以及“DATE”java 类型。我做错了什么?

【问题讨论】:

  • 如果您查看PostgreSQL documentation,您会看到date 类型不包括一天中的时间。你必须至少使用timestamp

标签: java spring hibernate postgresql jpa


【解决方案1】:

尝试像这样使用TemporalType.TIMESTAMP

 @Temporal(TemporalType.TIMESTAMP)
 private Date constructionYear;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-15
    • 2019-05-28
    • 1970-01-01
    • 2014-12-12
    • 1970-01-01
    • 2011-10-18
    • 1970-01-01
    相关资源
    最近更新 更多