こんにちは。SpringBoot&PostgreSQL11.4&JPAを使ってます。
このようなロジックでSampleテーブルにデータを入れようとしてます。
両方ともカラムの型はUUIDで2つの別テーブルをFKしてます。
Java
1list.forEach(ent -> { 2 Sample entity = new Sample(); 3 entity.setSample1Id(ent.getId()); 4 entity.setSample2Id(userId); 5 System.out.println("@@@@@"+entity.getSample1Id()); 6 System.out.println("@@@@@"+entity.getSample2Id()); 7 sampleRepository.save(entity); 8});
これを実行した結果がこの通りなのですが、JPAで実行されたログを見る限り、sample2_idがNullになってます。
log
1@@@@@9jIDz3BRQt-2HafxFETjLQ 2@@@@@oiZjDFxwQguqywfcgArbbg 3 4Hibernate: insert into sample (sample1_id, sample2_id, id) values (?, ?, ?) 5ERROR 17626 --- [nio-8080-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper : 6ERROR: null value in column "sample2_id" violates not-null constraint 7 詳細: Failing row contains (572ed1ca-ec9c-45a5-a70e-5ddce498e12a, null, aUser). 8ERROR 17626 --- [nio-8080-exec-3] o.h.i.ExceptionMapperStandardImpl : HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement] 9ERROR 17626 --- [nio-8080-exec-3] c.f.e.GlobalControllerExceptionHandler : could not execute statement; SQL [n/a]; constraint [sample2_id]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement 10
値自体はセットされており、Sample1Idの方はちゃんとUUID型になっているように見受けられます。なぜSample2IdはNullとなっているのでしょうか?JPAのEntityの定義によるものでしょうか?
(ちなみにSample1Id、Sample2Idは共にString型で定義してます)
アドバイスいただけたら幸いです。。
あなたの回答
tips
プレビュー