关于 Spring 数据 JDBC + Hikari + Postgres JSONB 的问题

Question about Spring Data JDBC + Hikari + Postgres JSONB

我正在尝试使用 Spring 启动数据 JDBC 和 Postgres 构建一个 API。

我有一个简单的 pojo 想写到 table:

public final class Test {
   private final String id;
   private final String definition;
}

table 有 2 列,一个 varchar id 列和一个 jsonb definition 列。

当使用简单的 CRUD 存储库保存 pojo 时,出现以下错误。

ERROR: column "definition" is of type jsonb but expression is of type character varying

这个问题的解决方案通常是在 application.yml 中传递 spring.datasourcehikari.connection-properties: stringtype=unspecified,但我无法让它工作。

当我为 hikari 激活调试日志时,我可以看到我为它激活的其他设置(比如池名称)

HikariConfig testPool - configuration:
HikariConfig allowPoolSuspension.............false
HikariConfig autoCommit......................true
HikariConfig catalog.........................none
HikariConfig connectionInitSql...............none
HikariConfig connectionTestQuery.............none
HikariConfig connectionTimeout...............30000
HikariConfig dataSource......................none
HikariConfig dataSourceClassName.............none
HikariConfig dataSourceJNDI..................none
HikariConfig dataSourceProperties............{password=<masked>}
HikariConfig driverClassName................."org.postgresql.Driver"
HikariConfig healthCheckProperties...........{}
HikariConfig healthCheckRegistry.............none
HikariConfig idleTimeout.....................600000
HikariConfig initializationFailTimeout.......1
HikariConfig isolateInternalQueries..........false
HikariConfig jdbcUrl.........................jdbc:postgresql://localhost/test
HikariConfig leakDetectionThreshold..........0
HikariConfig maxLifetime.....................1800000
HikariConfig maximumPoolSize.................10
HikariConfig metricRegistry..................none
HikariConfig metricsTrackerFactory...........none
HikariConfig minimumIdle.....................10
HikariConfig password........................<masked>
HikariConfig poolName........................"testPool"
HikariConfig readOnly........................false
HikariConfig registerMbeans..................false
HikariConfig scheduledExecutor...............none
HikariConfig schema..........................none
HikariConfig threadFactory...................internal
HikariConfig transactionIsolation............default
HikariConfig username........................"test"
HikariConfig validationTimeout...............5000
HikariDataSource testPool - Starting...
HikariPool testPool - Added connection org.postgresql.jdbc.PgConnection@49ba49a7
HikariDataSource testPool - Start completed.

这是我的 application.yml:

spring:
  datasource:
    url: jdbc:postgresql://localhost/test
    username: test
    hikari:
      connection-properties: stringtype=unspecified
      poolName: testPool
logging.level.com.zaxxer.hikari: DEBUG

有什么想法吗?

编辑:

这是存储库界面:

@Repository
interface TestRepository extends org.springframework.data.repository.<Test, String>{
}

我认为您需要在属性文件中使用数据源属性,如下所示

spring.datasource.hikari.data-source-properties = stringtype=unspecified

然后在"definition"的getter上面添加两个注释。

@基本 @Enumerated(EnumType.STRING) public 字符串 getDefinition() { return定义; }