how to store PostgreSQL jsonb using SpringBoot + JPA?

后端 未结 4 1750
不知归路
不知归路 2021-02-01 10:23

I\'m working on a migration software that will consume unknown data from REST services.

I already think about use MongoDB but I decide to not use it and use PostgreSQL.<

4条回答
  •  孤独总比滥情好
    2021-02-01 11:03

    You are making things overly complex by adding Spring Data JPA just to execute a simple insert statement. You aren't using any of the JPA features. Instead do the following

    1. Replace spring-boot-starter-data-jpa with spring-boot-starter-jdbc
    2. Remove your DnitRepository interface
    3. Inject JdbcTemplate where you where injecting DnitRepository
    4. Replace dnitRepository.insertdata(2, someJsonDataAsString ); with jdbcTemplate.executeUpdate("insert into dnit(id, data) VALUES (?,to_json(?))", id, data);

    You were already using plain SQL (in a very convoluted way), if you need plain SQL (and don't have need for JPA) then just use SQL.

    Ofcourse instead of directly injecting the JdbcTemplate into your controller you probably want to hide that logic/complexity in a repository or service.

提交回复
热议问题