【问题标题】:UUID support for different DB with Spring boot使用 Spring Boot 对不同 DB 的 UUID 支持
【发布时间】:2021-05-09 01:53:16
【问题描述】:

我正在尝试使用 UUID 作为我的表的主键。我使用 postgresql 进行生产,使用 h2 进行测试。我正在使用 liquibase 创建表并将此列类型设置为 UUID,其中 liquibase 支持 h2 和 postgres 的类型

<column name="uuid" type="UUID">

 @Id
 private UUID uuid;

当我尝试使用带有 spring-boot 的 JPA 保存记录时,我遇到了两个问题/问题。对于 Postgres,我会收到以下错误

org.postgresql.util.PSQLException: ERROR: operator does not exist: uuid = bytea

我知道这是因为 JPA 不知道应该从 Java UUID 类型转换为哪种类型。有人建议在实体类中归档的 UUID 上使用 @Type(type="org.hibernate.type.PostgresUUIDType") 。但是,这对我没有任何改变,我仍然遇到同样的错误。不知道我错过了什么。

另一个问题是,当我切换到运行测试配置时,如何使它也与 h2 一起工作,因为注释特定于 Postgres。请问有什么建议吗?

【问题讨论】:

    标签: postgresql spring-boot jpa h2 uuid


    【解决方案1】:

    Hibernate 无法将 java UUID 映射到 postgres UUID。 确保在 appication.yaml 中使用正确的方言

    spring:
      jpa:
        database-platform: org.hibernate.dialect.PostgreSQL10Dialect
    

    对于本地配置文件,您可以使用以下内容

    spring:
      datasource:
        driverClassName: org.h2.Driver
        username: sa
        password: password
        url: jdbc:h2:mem:testdb;MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE
    

    【讨论】:

      猜你喜欢
      • 2022-12-12
      • 2015-03-03
      • 1970-01-01
      • 2021-04-10
      • 1970-01-01
      • 2023-03-14
      • 2022-12-23
      • 2015-07-22
      • 1970-01-01
      相关资源
      最近更新 更多