spring - 使用 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.
我可以为这个领域编写一个转换器,但应该有一个标准的方法,感觉就像我在寻找一些我发现非常接近的实际工作的实现的例子时遗漏了一些东西。
解决方案
经过一番研究,我发现了这个页面,使用官方 postgres 文档中的 Java 8 Date and Time classes并将属性类型更改为 LocalDateTime 而不是 Instant:
java
@Column(name = "meal_time", nullable = false)
private LocalDateTime mealTime = LocalDateTime.now();
这解决了这个问题,并且是这个特定项目中可接受的解决方案。
推荐阅读
- java - Netty ByteBuf 到 byte[] 解码器
- java - 为什么在这种情况下需要 catch IOException
- c++98 - 如何使用 std::copy 将给定的向量范围正确复制到另一个向量?
- python - 使用参数替换将 bash 环境变量集读取到 Python 变量中
- c# - 在 asp.net core 中创建作用域工厂
- tensorflow - tf.gradients 会通过 tf.cond 吗?
- c# - 在 Unity 中创建二维箭头
- python - 如何使用 tkinter 全屏显示帧?(Python)
- c# - 防止在 Wpf RichTextBox 中删除文本
- javascript - 如何使用 Javascript 向 .NetCore 服务器创建帖子