Question about Spring Data JDBC + Hikari + Postgres JSONB

陌路散爱 提交于 2021-02-11 10:11:21

问题


I'm trying to build an API using Spring Boot Data JDBC with Postgres.

I have a simple pojo that I wish to write to a table:

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

The table has 2 columns, a varchar id column and a jsonb definition column.

When using a simple CRUD repository to save the pojo, I get the following error.

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

The solution for this would normally be to pass spring.datasourcehikari.connection-properties: stringtype=unspecified in application.yml, but I can't get this to work.

When I activate the debug logging for hikari I can see other settings I activate for it (like the pool name)

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.

This is my application.yml:

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

Any ideas? 🙏

Edit:

This is the repository interface:

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

回答1:


I think you need to use data-source-properties in properties file as below

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

Then add two annotations above getter of "definition".

@Basic @Enumerated(EnumType.STRING) public String getDefinition() { return definition; }



来源:https://stackoverflow.com/questions/53392146/question-about-spring-data-jdbc-hikari-postgres-jsonb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!