【问题标题】:Spring JPA Hibernate giving error updating timestamp wo timezone in postgresSpring JPA Hibernate在postgres中给出错误更新时间戳wo时区
【发布时间】:2018-11-18 15:23:11
【问题描述】:

我一直在尝试使用更新 stmt 更新时间戳字段。我尝试了 java.sql.timestamp、java.util.date、Calendar、LocalDateTime、ZonedDateTime 和一堆其他 java date util 包。他们似乎都没有工作。

列:commit_ts(Postgresql 中没有时区的时间戳)在我们的 JPA/Hibernate 中定义为

@Column(name = "COMMIT_TS")
@Temporal(TemporalType.TIMESTAMP)
private Timestamp commitTs;

这里是查询

@Timed(name = "updateWorkAllocationStatus")
@Transactional
@Modifying(clearAutomatically = true)
@Query(
      nativeQuery = true,
      value = "UPDATE wlm_work_allocation SET commit_ts=:ts " + 
              "WHERE allocation_id = :allocationId " + 
              "and status = :status " + 
              "and commit_ts == null"
)
int updateWorkAllocationStatus(
    @Param("timestamp") Timestamp ts,
    @Param("allocationId")Long allocationId,
    @Param("status")String status
);

我也尝试过 NativeQueries

@NamedNativeQuery(name = "WorkAllocationEntity.updateCommitTs",
        query="UPDATE wlm_work_allocation SET commit_ts= TIMESTAMP WHERE allocation_id=:allocationId and status=:status and commit_ts==null")

注意:根据这个链接,SQL 标准要求只写时间戳等同于没有时区的时间戳,PostgreSQL 尊重这种行为。 https://www.postgresql.org/docs/9.1/static/datatype-datetime.html

Springboot版本:1.5.7.RELEASE

Postgres JDBC 驱动程序:9.0-801.jdbc4

PostgresSQL 数据库:9.6.5

Error: 
org.postgresql.util.PSQLException: ERROR: operator does not 
exist: timestamp without time zone == unknown
Hint: No operator matches the given name and argument type(s). 
You might need to add explicit type casts.

谁能帮忙?

【问题讨论】:

    标签: postgresql hibernate spring-boot spring-data-jpa


    【解决方案1】:

    JPQL 和 SQL 中的比较运算符是 = 而不是 ==

    commit_ts==null 
    

    应该是

    commit_ts is null
    

    【讨论】:

    • 啊。我没有看过那个天使。该错误具有误导性。谢谢你的反馈。让我试着更新你。再次感谢您
    • 您的意见很有价值。我可以要求您编辑 commit_ts IS NULL 的答案吗?(空检查的 SQL 方式)我应该小心一点,但我很傻。这个答案会帮助别人
    • 好的。我已经编辑了答案。请您将答案标记为正确吗?
    猜你喜欢
    • 2016-09-17
    • 2020-09-11
    • 2020-01-20
    • 2013-12-22
    • 2017-04-20
    • 2022-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多