使用Spring数据的postgres中的时间戳错误:列$ COLUMN_NAME的类型为timestamp,没有时区,但表达式的类型为bytea

gro*_*roo 5 postgresql spring hibernate jpa spring-boot

使用以下内容:

Oracle JDK 1.8.0._171

Spring Boot 1.5.9-发布

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.
Run Code Online (Sandbox Code Playgroud)

我可以为这个领域编写一个转换器,但是应该有一种标准的方法,感觉就像是我在寻找一些实例的过程中缺少了一些东西,这些实例我发现非常接近的实现确实有效。

gro*_*roo 5

经过一番研究,我找到了这个页面,Using Java 8 Date and Time classes from official postgres documentation并将属性类型更改为LocalDateTime而不是Instant:

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

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