问题描述
我无法为下面描述的字段设置空值。我想插入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实现。
解决方法
我认为这是一个已知问题。引用链接
- https://bugs.eclipse.org/bugs/show_bug.cgi?id=535431-引发的错误
- https://bugs.eclipse.org/bugs/show_bug.cgi?id=546312-已修复的错误
- https://www.postgresql.org/messageid/200412101610.16959.scrawford%40pinpointresearch.com
- https://github.com/typeorm/typeorm/issues/2290
(typeorm也面临同样的问题)。
是的,错误消息来自postgres jdbc驱动程序。原因 对于此错误是使用varchar作为“空”数据类型的eclipselink 值。其他驱动程序可能会或可能不会忽略此错误的数据类型, postgres驱动程序没有。
当您尝试将answerDate设置为null时,它将变成“ null”字符串,该字符串将提交到postgres数据库,并由于“ null”不是有效的时间戳格式而导致此错误。
最好在不存在LocalDateTime的情况下分配默认值。
private LocalDateTime answerDate = LocalDateTime.now();
在这种情况下,此错误消息显示“是时间戳类型...但是 表达式是文本类型。.“可以避免。
更新 该错误已在eclipseLink版本2.7.6(https://github.com/eclipse-ee4j/eclipselink/pull/415)中修复
,也许它对某人有用。 我遇到了同样的错误(我使用了 EclipseLink、Websphere 和 PostgreSQL)。 将 EclipseLink 更新到 2.7.8 后,我继续遇到相同的错误。 经过几个小时的搜索,我找到了以下解决方案:
将属性添加到 server.xml
中的 dataSourcestringType="unspecified"
或设置 stringType=unspecified
作为 Postgres URL 的查询参数