1

我无法为下面描述的字段设置空值。我想插入 NULL 作为默认值。

@Column(name = "answer_date",columnDefinition = "TIMESTAMP")
private LocalDateTime answerDate

当我设置 NULL 时,它显示以下错误:

Caused by: org.postgresql.util.PSQLException: ERROR: column "answer_date" is of type timestamp without time zone but expression is of type character varying 

我使用 EclipseLink 作为 JPA 实现。

4

2 回答 2

2

我认为这是一个已知问题。参考链接

(typeorm 也面临同样的问题)。

是的,错误消息来自 postgres jdbc 驱动程序。此错误的原因是 eclipselink 使用 varchar 作为“null”值的数据类型。其他驱动程序可能会或可能不会忽略这个错误的数据类型,postgres 驱动程序不会。

当您尝试将 answerDate 设置为 null 时,它会变成一个“null”字符串,该字符串会提交到 postgres 数据库并导致此错误,因为“null”不是有效的时间戳格式。

如果您指定一个默认值以防 LocalDateTime 不存在,则效果会更好。

private LocalDateTime answerDate = LocalDateTime.now();

在这种情况下,可以避免出现“时间戳类型......但表达式类型文本......”的错误消息。

更新 eclipseLink 2.7.6 版中的错误已修复(https://github.com/eclipse-ee4j/eclipselink/pull/415

于 2020-09-07T10:54:11.900 回答
0

也许它对某人有用。我遇到了同样的错误(我使用了 EclipseLink、Websphere 和 PostgreSQL)。将 EclipseLink 更新到 2.7.8 后,我继续遇到同样的错误。经过几个小时的搜索,我找到了以下解决方案:

server.xml中向dataSource添加属性

stringType="unspecified"

或设置stringType=unspecified为 Postgres URL 的查询参数

于 2021-06-29T06:55:01.313 回答