首页 > 解决方案 > 使用 Spring Data 的 postgres 中的时间戳出错:列 $COLUMN_NAME 是没有时区的时间戳类型,但表达式是 bytea 类型

问题描述

使用以下内容:

甲骨文 JDK 1.8.0._171

Spring Boot 1.5.9-RELEASE

PostgreSQL 9.6

Postgres 驱动程序:42.2.4

从以下定义中获取错误:

表格栏: sql meal_time TIMESTAMP DEFAULT now() NOT NULL

实体属性定义: java @Column(name = "meal_time", nullable=false) private Instant mealTime = Instant.now();

每当我尝试刷新或查询实体时都会引发错误:

Caused by: org.postgresql.util.PSQLException: ERROR: column "meal_time" is of type timestamp without time zone but expression is of type bytea
  Hint: You will need to rewrite or cast the expression.

我可以为这个领域编写一个转换器,但应该有一个标准的方法,感觉就像我在寻找一些我发现非常接近的实际工作的实现的例子时遗漏了一些东西。

标签: springpostgresqlhibernatespring-bootjpa

解决方案


经过一番研究,我发现了这个页面,使用官方 postgres 文档中的 Java 8 Date and Time classes并将属性类型更改为 LocalDateTime 而不是 Instant:

java @Column(name = "meal_time", nullable = false) private LocalDateTime mealTime = LocalDateTime.now();

这解决了这个问题,并且是这个特定项目中可接受的解决方案。


推荐阅读