【问题标题】:Postgres + Spring Boot JPA - store timestamp with nanosecond precisionPostgres + Spring Boot JPA - 以纳秒精度存储时间戳
【发布时间】:2023-02-10 17:21:29
【问题描述】:

Postgres 不支持低于 1 微秒的时间戳精度。建议的解决方案是store timestamp across two separate fields: one with timestamp up to second precision and separately nanoseconds。我的问题是如何在 Spring Boot 中实现它,以便我仍然可以拥有纳秒级精度的字段。如何将字段存入数据库时​​一分为二,如何基于两列正确的hydrate对象中的字段?

我目前使用的是 Spring Boot 2.5.14、Java 11 或 Kotlin 1.6(具体取决于项目)和 Postgres 14.4。如果有需要升级的解决方案,我们可能可以做到,无论如何都计划升级。

【问题讨论】:

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


    【解决方案1】:

    您可以为 BigDecimal/BigIntegerInstant 创建一个 AttributeConverter,并将 UTC 时间戳存储为 numeric(28,9)/numeric(28,0),这应该足以存储时间戳。请注意,在时间戳算术函数中使用该字段之前,您必须转换为时间戳类型。

    【讨论】:

      猜你喜欢
      • 2021-01-23
      • 2023-03-17
      • 1970-01-01
      • 2017-04-22
      • 1970-01-01
      • 2023-04-07
      • 2015-02-27
      • 2016-07-14
      • 2011-07-08
      相关资源
      最近更新 更多