2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Spring Data JDBCでInsertできなかった件のメモ

Posted at

なんとなく復習してたSpring Data JDBCでハマったのでメモ。
(ベースにしたSpring bootがちょっと古いけど・・。)

##環境

  • Spring boot 2.1.2
  • Spring Data JDBC 1.0.4
  • PostgreSQL 10.5

##やろうとしたこと
単に、Spring Data JDBCでテーブルにオブジェクトを保存(レコードをInsert)しようとしただけ

##ハマった原因
どうも、Spring Data JDBCは@IDをつけたフィールドがオブジェクト型の時はnullのときはInsert、nullじゃないときはupdateでSQLを発行するらしい。
テーブルのPKがAuto Generatedじゃないとだめよってことね。

参考
https://stackoverflow.com/questions/50371775/why-does-spring-data-jdbc-not-save-my-car-object

##サンプルコードで動かしてみる
オブジェクト

Users.java
public class Users {
  @Id
  private Integer user_id;
  private String password;
  private String notes;

サービス

SampleService.java
@Transactional
@Service
public class SampleService {
  @Autowired
  private UserRepository dao;
//略
//極めて雑なコードだけど・・
  public void add(Integer id) {
    LocalDateTime time = LocalDateTime.now();
    Users user = new Users(id, "password", time.toString());
    logger.info(user.toString());
    Users inserted = dao.save(user);
  }

テスト実行用

SampleServiceTest.java
@RunWith(SpringRunner.class)
@SpringBootTest
public class SampleServiceTest {
  
  @Autowired
  private SampleService service;
  
//こちらを実行するとUpdate
  @Test
  public void test1() {
    service.add(3);
  }
  
//こちらを実行するとInsert
  @Test
  public void test2() {
    service.add(null);
  }
}

test1()実行時

2019-11-19 18:41:47.275  INFO 37316 --- [           main] org.example.test.service.SampleService   : userid: 3, note: 2019-11-19T18:41:47.274

そのときのPostgreのSQLトレース

2019-11-19 09:41:47.326 UTC [167] LOG:  execute <unnamed>: BEGIN
2019-11-19 09:41:47.334 UTC [167] LOG:  execute <unnamed>: UPDATE users SET user_id = $1, password = $2, notes = $3 WHERE user_id = $4
2019-11-19 09:41:47.334 UTC [167] DETAIL:  parameters: $1 = '3', $2 = 'password', $3 = '2019-11-19T18:41:47.274', $4 = '3'

test2()実行時

2019-11-19 18:41:47.355  INFO 37316 --- [           main] org.example.test.service.SampleService   : userid: null, note: 2019-11-19T18:41:47.355

そのときのPostgreのSQLトレース

2019-11-19 09:41:47.368 UTC [167] LOG:  execute <unnamed>: BEGIN
2019-11-19 09:41:47.368 UTC [167] LOG:  execute <unnamed>: INSERT INTO users (password, notes) VALUES ($1, $2)
	RETURNING *
2019-11-19 09:41:47.368 UTC [167] DETAIL:  parameters: $1 = 'password', $2 = '2019-11-19T18:41:47.355'

##さいごに
とりあえずDDLを変えるってのと、Spring bootのバージョンも上げてみようかな。
Postgre10だとAuto Generetedにできるのね。
https://qiita.com/nuko_yokohama/items/7d0d5525bcefaa3332ce

あ、PostgreSQLのSQLトレースの設定はこちらを参考にした。
https://www.kakiro-web.com/postgresql/postgresql-sql-log-system.html

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?