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

时间:2018-07-23 21:07:18

标签: spring postgresql hibernate spring-boot jpa

使用以下内容:

Oracle 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.

我可以为该领域编写一个转换器,但是应该有一种标准的方法,感觉到我在寻找一些实例,但发现它们确实很有效,但似乎缺少了一些东西。

2 个答案:

答案 0 :(得分:1)

经过一番研究,我发现Postgres官方文档中的this page, Using Java 8 Date and Time classes并将属性类型更改为LocalDateTime而不是Instant:

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

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

答案 1 :(得分:0)

正如@crizzis所指出的那样,从5.2版开始,在Hibernate核心模块org.hibernate:hibernate-corehttps://mvnrepository.com/artifact/org.hibernate/hibernate-core)中包括Java 8类型映射,包括从java.time.Instantorg.hibernate:hibernate-java8的映射。如果使用5.1之前的休眠核心版本,则这些类型映射包含在特殊模块compile group: 'org.hibernate', name: 'hibernate-core', version: '5.4.16.Final' https://mvnrepository.com/artifact/org.hibernate/hibernate-java8)中。请注意,此模块从5.2版(已移至hibernate-core)开始为空,因此我建议使用5.1.17版。最终版,例如

.then()