dirty check 원인 분석하기

dasd412·2022년 5월 15일
0

포트폴리오

목록 보기
39/41

이전 포스트 링크

https://velog.io/@dasd412/1.%EB%8D%B0%EC%9D%B4%ED%84%B0-%EB%B0%8F-%EA%B5%AC%EC%A1%B0-%EC%84%A4%EA%B3%84-1N-%EA%B4%80%EA%B3%84-%EC%97%94%ED%8B%B0%ED%8B%B0-save%EC%8B%9C-update-%EC%BF%BC%EB%A6%AC%EA%B0%80-%EC%B6%94%EA%B0%80%EB%A1%9C-%EB%B0%9C%EC%83%9D%ED%95%98%EB%8A%94-%EC%9D%B4%EC%8A%88-%ED%95%B4%EA%B2%B0

위 링크는 생성 시간과 수정 시간을 자동으로 추적해주는 BaseTimeEntity를 사용했을 때 발생했던 이슈(불필요한 Update 쿼리 생성)에 대한 것이다.


면접 때 받았던 질문

면접 때 받았던 질문은 '기술적으로 가장 어려웠던 문제는 무엇인가요?'라는 것이였다. 그 질문에 대한 답으로 위 링크의 이슈를 설명했다.
그리고 해결책으로 문제의 원인인 BaseTimeEntity제거한 것으로 끝냈다고 했다.
하지만 이런 방법은 성능 상 문제를 발견한 것과 원인 추적은 그렇다 치더라도, 해결책이 너무 무식한 방법이였다.

예를 들어, 생성 시간갱신 시간에 대해 추적할 필요가 있다는 요구사항이 발생했다고 하자. 그러면 어떻게 해야 할 것인가? (와 같은 후속적인 질문을 떠올리지 못했다.)

일단 생성 시간갱신 시간을 추적해야 한다면, 어느 엔티티를 추적해야 할까?

처음에는 모든 엔티티에 대해 추적했지만, 사실 식단과 음식은 일지 엔티티에 완전 종속되기 때문에 두 하위 엔티티는 추적할 필요가 없다. 따라서 작성자와 일지 엔티티만 추적하면 된다.


시도한 방법

작성자와 일지에만 BaseTimeEntity 를 상속시켰다.
그런데 테스트 코드 로그 뿐만 아니라, 실제 어플리케이션 실행 로그에서도 추가적인 update 쿼리가 발생하지 않았다.

이전 포스트 코드에 대한 실험

findHigherThanBloodSugarBetweenTime()에 대해 실험한 로그는 다음과 같다.

2022-05-15 11:14:08.663  INFO 15592 --- [           main] c.d.r.a.service.domain.SaveDiaryService  : saveWriter
Hibernate: select max(writer0_.writer_id) as col_0_0_ from writer writer0_
Hibernate: select writer0_.writer_id as writer_i1_4_1_, writer0_.created_at as created_2_4_1_, writer0_.updated_at as updated_3_4_1_, writer0_.email as email4_4_1_, writer0_.name as name5_4_1_, writer0_.password as password6_4_1_, writer0_.profile_id as profile10_4_1_, writer0_.provider as provider7_4_1_, writer0_.provider_id as provider8_4_1_, writer0_.role as role9_4_1_, diaries1_.writer_id as writer_i2_0_3_, diaries1_.diary_id as diary_id1_0_3_, diaries1_.diary_id as diary_id1_0_0_, diaries1_.writer_id as writer_i2_0_0_, diaries1_.created_at as created_3_0_0_, diaries1_.updated_at as updated_4_0_0_, diaries1_.fpg as fpg5_0_0_, diaries1_.remark as remark6_0_0_, diaries1_.written_time as written_7_0_0_ from writer writer0_ left outer join diabetes_diary diaries1_ on writer0_.writer_id=diaries1_.writer_id where writer0_.writer_id=?
2022-05-15 11:14:08.664  INFO 15592 --- [           main] c.d.r.a.service.domain.SaveDiaryService  : saveDiaryOfWriterById
Hibernate: insert into writer (created_at, updated_at, email, name, password, profile_id, provider, provider_id, role, writer_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: select max(diabetesdi0_.diary_id) as col_0_0_ from diabetes_diary diabetesdi0_
Hibernate: select diabetesdi0_.diary_id as diary_id1_0_1_, diabetesdi0_.writer_id as writer_i2_0_1_, diabetesdi0_.created_at as created_3_0_1_, diabetesdi0_.updated_at as updated_4_0_1_, diabetesdi0_.fpg as fpg5_0_1_, diabetesdi0_.remark as remark6_0_1_, diabetesdi0_.written_time as written_7_0_1_, dietlist1_.diary_id as diary_id4_1_3_, dietlist1_.writer_id as writer_i5_1_3_, dietlist1_.diary_id as diary_id0_1_3_, dietlist1_.writer_id as writer_i0_1_3_, dietlist1_.diet_id as diet_id1_1_3_, dietlist1_.diary_id as diary_id0_1_0_, dietlist1_.writer_id as writer_i0_1_0_, dietlist1_.diet_id as diet_id1_1_0_, dietlist1_.diary_id as diary_id4_1_0_, dietlist1_.writer_id as writer_i5_1_0_, dietlist1_.blood_sugar as blood_su2_1_0_, dietlist1_.eat_time as eat_time3_1_0_ from diabetes_diary diabetesdi0_ left outer join diet dietlist1_ on diabetesdi0_.diary_id=dietlist1_.diary_id and diabetesdi0_.writer_id=dietlist1_.writer_id where diabetesdi0_.diary_id=? and diabetesdi0_.writer_id=?
2022-05-15 11:14:08.667  INFO 15592 --- [           main] c.d.r.a.service.domain.SaveDiaryService  : saveDiaryOfWriterById
Hibernate: insert into diabetes_diary (created_at, updated_at, fpg, remark, written_time, diary_id, writer_id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: select max(diabetesdi0_.diary_id) as col_0_0_ from diabetes_diary diabetesdi0_
Hibernate: select diabetesdi0_.diary_id as diary_id1_0_1_, diabetesdi0_.writer_id as writer_i2_0_1_, diabetesdi0_.created_at as created_3_0_1_, diabetesdi0_.updated_at as updated_4_0_1_, diabetesdi0_.fpg as fpg5_0_1_, diabetesdi0_.remark as remark6_0_1_, diabetesdi0_.written_time as written_7_0_1_, dietlist1_.diary_id as diary_id4_1_3_, dietlist1_.writer_id as writer_i5_1_3_, dietlist1_.diary_id as diary_id0_1_3_, dietlist1_.writer_id as writer_i0_1_3_, dietlist1_.diet_id as diet_id1_1_3_, dietlist1_.diary_id as diary_id0_1_0_, dietlist1_.writer_id as writer_i0_1_0_, dietlist1_.diet_id as diet_id1_1_0_, dietlist1_.diary_id as diary_id4_1_0_, dietlist1_.writer_id as writer_i5_1_0_, dietlist1_.blood_sugar as blood_su2_1_0_, dietlist1_.eat_time as eat_time3_1_0_ from diabetes_diary diabetesdi0_ left outer join diet dietlist1_ on diabetesdi0_.diary_id=dietlist1_.diary_id and diabetesdi0_.writer_id=dietlist1_.writer_id where diabetesdi0_.diary_id=? and diabetesdi0_.writer_id=?
2022-05-15 11:14:08.668  INFO 15592 --- [           main] c.d.r.a.service.domain.SaveDiaryService  : saveDiaryOfWriterById
Hibernate: insert into diabetes_diary (created_at, updated_at, fpg, remark, written_time, diary_id, writer_id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: select max(diabetesdi0_.diary_id) as col_0_0_ from diabetes_diary diabetesdi0_
Hibernate: select diabetesdi0_.diary_id as diary_id1_0_1_, diabetesdi0_.writer_id as writer_i2_0_1_, diabetesdi0_.created_at as created_3_0_1_, diabetesdi0_.updated_at as updated_4_0_1_, diabetesdi0_.fpg as fpg5_0_1_, diabetesdi0_.remark as remark6_0_1_, diabetesdi0_.written_time as written_7_0_1_, dietlist1_.diary_id as diary_id4_1_3_, dietlist1_.writer_id as writer_i5_1_3_, dietlist1_.diary_id as diary_id0_1_3_, dietlist1_.writer_id as writer_i0_1_3_, dietlist1_.diet_id as diet_id1_1_3_, dietlist1_.diary_id as diary_id0_1_0_, dietlist1_.writer_id as writer_i0_1_0_, dietlist1_.diet_id as diet_id1_1_0_, dietlist1_.diary_id as diary_id4_1_0_, dietlist1_.writer_id as writer_i5_1_0_, dietlist1_.blood_sugar as blood_su2_1_0_, dietlist1_.eat_time as eat_time3_1_0_ from diabetes_diary diabetesdi0_ left outer join diet dietlist1_ on diabetesdi0_.diary_id=dietlist1_.diary_id and diabetesdi0_.writer_id=dietlist1_.writer_id where diabetesdi0_.diary_id=? and diabetesdi0_.writer_id=?
2022-05-15 11:14:08.669  INFO 15592 --- [           main] c.d.r.a.service.domain.SaveDiaryService  : saveDietOfWriterById
Hibernate: insert into diabetes_diary (created_at, updated_at, fpg, remark, written_time, diary_id, writer_id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: select diabetesdi0_.diary_id as diary_id1_0_, diabetesdi0_.writer_id as writer_i2_0_, diabetesdi0_.created_at as created_3_0_, diabetesdi0_.updated_at as updated_4_0_, diabetesdi0_.fpg as fpg5_0_, diabetesdi0_.remark as remark6_0_, diabetesdi0_.written_time as written_7_0_ from diabetes_diary diabetesdi0_ where diabetesdi0_.writer_id=? and diabetesdi0_.diary_id=?
Hibernate: select max(diet0_.diet_id) as col_0_0_ from diet diet0_
Hibernate: select diet0_.diary_id as diary_id0_1_1_, diet0_.writer_id as writer_i0_1_1_, diet0_.diet_id as diet_id1_1_1_, diet0_.diary_id as diary_id4_1_1_, diet0_.writer_id as writer_i5_1_1_, diet0_.blood_sugar as blood_su2_1_1_, diet0_.eat_time as eat_time3_1_1_, foodlist1_.diary_id as diary_id5_2_3_, foodlist1_.writer_id as writer_i6_2_3_, foodlist1_.diet_id as diet_id7_2_3_, foodlist1_.diary_id as diary_id0_2_3_, foodlist1_.writer_id as writer_i0_2_3_, foodlist1_.diet_id as diet_id0_2_3_, foodlist1_.food_id as food_id1_2_3_, foodlist1_.diary_id as diary_id0_2_0_, foodlist1_.writer_id as writer_i0_2_0_, foodlist1_.diet_id as diet_id0_2_0_, foodlist1_.food_id as food_id1_2_0_, foodlist1_.diary_id as diary_id5_2_0_, foodlist1_.writer_id as writer_i6_2_0_, foodlist1_.diet_id as diet_id7_2_0_, foodlist1_.amount as amount2_2_0_, foodlist1_.amount_unit as amount_u3_2_0_, foodlist1_.food_name as food_nam4_2_0_ from diet diet0_ left outer join food foodlist1_ on diet0_.diary_id=foodlist1_.diary_id and diet0_.writer_id=foodlist1_.writer_id and diet0_.diet_id=foodlist1_.diet_id where diet0_.diary_id=? and diet0_.writer_id=? and diet0_.diet_id=?
2022-05-15 11:14:08.670  INFO 15592 --- [           main] c.d.r.a.service.domain.SaveDiaryService  : saveDietOfWriterById
Hibernate: select diabetesdi0_.diary_id as diary_id1_0_, diabetesdi0_.writer_id as writer_i2_0_, diabetesdi0_.created_at as created_3_0_, diabetesdi0_.updated_at as updated_4_0_, diabetesdi0_.fpg as fpg5_0_, diabetesdi0_.remark as remark6_0_, diabetesdi0_.written_time as written_7_0_ from diabetes_diary diabetesdi0_ where diabetesdi0_.writer_id=? and diabetesdi0_.diary_id=?
Hibernate: insert into diet (blood_sugar, eat_time, diary_id, writer_id, diet_id) values (?, ?, ?, ?, ?)
Hibernate: select max(diet0_.diet_id) as col_0_0_ from diet diet0_
Hibernate: select diet0_.diary_id as diary_id0_1_1_, diet0_.writer_id as writer_i0_1_1_, diet0_.diet_id as diet_id1_1_1_, diet0_.diary_id as diary_id4_1_1_, diet0_.writer_id as writer_i5_1_1_, diet0_.blood_sugar as blood_su2_1_1_, diet0_.eat_time as eat_time3_1_1_, foodlist1_.diary_id as diary_id5_2_3_, foodlist1_.writer_id as writer_i6_2_3_, foodlist1_.diet_id as diet_id7_2_3_, foodlist1_.diary_id as diary_id0_2_3_, foodlist1_.writer_id as writer_i0_2_3_, foodlist1_.diet_id as diet_id0_2_3_, foodlist1_.food_id as food_id1_2_3_, foodlist1_.diary_id as diary_id0_2_0_, foodlist1_.writer_id as writer_i0_2_0_, foodlist1_.diet_id as diet_id0_2_0_, foodlist1_.food_id as food_id1_2_0_, foodlist1_.diary_id as diary_id5_2_0_, foodlist1_.writer_id as writer_i6_2_0_, foodlist1_.diet_id as diet_id7_2_0_, foodlist1_.amount as amount2_2_0_, foodlist1_.amount_unit as amount_u3_2_0_, foodlist1_.food_name as food_nam4_2_0_ from diet diet0_ left outer join food foodlist1_ on diet0_.diary_id=foodlist1_.diary_id and diet0_.writer_id=foodlist1_.writer_id and diet0_.diet_id=foodlist1_.diet_id where diet0_.diary_id=? and diet0_.writer_id=? and diet0_.diet_id=?
2022-05-15 11:14:08.671  INFO 15592 --- [           main] c.d.r.a.service.domain.SaveDiaryService  : saveDietOfWriterById
Hibernate: select diabetesdi0_.diary_id as diary_id1_0_, diabetesdi0_.writer_id as writer_i2_0_, diabetesdi0_.created_at as created_3_0_, diabetesdi0_.updated_at as updated_4_0_, diabetesdi0_.fpg as fpg5_0_, diabetesdi0_.remark as remark6_0_, diabetesdi0_.written_time as written_7_0_ from diabetes_diary diabetesdi0_ where diabetesdi0_.writer_id=? and diabetesdi0_.diary_id=?
Hibernate: insert into diet (blood_sugar, eat_time, diary_id, writer_id, diet_id) values (?, ?, ?, ?, ?)
Hibernate: select max(diet0_.diet_id) as col_0_0_ from diet diet0_
Hibernate: select diet0_.diary_id as diary_id0_1_1_, diet0_.writer_id as writer_i0_1_1_, diet0_.diet_id as diet_id1_1_1_, diet0_.diary_id as diary_id4_1_1_, diet0_.writer_id as writer_i5_1_1_, diet0_.blood_sugar as blood_su2_1_1_, diet0_.eat_time as eat_time3_1_1_, foodlist1_.diary_id as diary_id5_2_3_, foodlist1_.writer_id as writer_i6_2_3_, foodlist1_.diet_id as diet_id7_2_3_, foodlist1_.diary_id as diary_id0_2_3_, foodlist1_.writer_id as writer_i0_2_3_, foodlist1_.diet_id as diet_id0_2_3_, foodlist1_.food_id as food_id1_2_3_, foodlist1_.diary_id as diary_id0_2_0_, foodlist1_.writer_id as writer_i0_2_0_, foodlist1_.diet_id as diet_id0_2_0_, foodlist1_.food_id as food_id1_2_0_, foodlist1_.diary_id as diary_id5_2_0_, foodlist1_.writer_id as writer_i6_2_0_, foodlist1_.diet_id as diet_id7_2_0_, foodlist1_.amount as amount2_2_0_, foodlist1_.amount_unit as amount_u3_2_0_, foodlist1_.food_name as food_nam4_2_0_ from diet diet0_ left outer join food foodlist1_ on diet0_.diary_id=foodlist1_.diary_id and diet0_.writer_id=foodlist1_.writer_id and diet0_.diet_id=foodlist1_.diet_id where diet0_.diary_id=? and diet0_.writer_id=? and diet0_.diet_id=?
2022-05-15 11:14:08.672  INFO 15592 --- [           main] c.d.r.a.service.domain.SaveDiaryService  : saveDietOfWriterById
Hibernate: select diabetesdi0_.diary_id as diary_id1_0_, diabetesdi0_.writer_id as writer_i2_0_, diabetesdi0_.created_at as created_3_0_, diabetesdi0_.updated_at as updated_4_0_, diabetesdi0_.fpg as fpg5_0_, diabetesdi0_.remark as remark6_0_, diabetesdi0_.written_time as written_7_0_ from diabetes_diary diabetesdi0_ where diabetesdi0_.writer_id=? and diabetesdi0_.diary_id=?
Hibernate: insert into diet (blood_sugar, eat_time, diary_id, writer_id, diet_id) values (?, ?, ?, ?, ?)
Hibernate: select max(diet0_.diet_id) as col_0_0_ from diet diet0_
Hibernate: select diet0_.diary_id as diary_id0_1_1_, diet0_.writer_id as writer_i0_1_1_, diet0_.diet_id as diet_id1_1_1_, diet0_.diary_id as diary_id4_1_1_, diet0_.writer_id as writer_i5_1_1_, diet0_.blood_sugar as blood_su2_1_1_, diet0_.eat_time as eat_time3_1_1_, foodlist1_.diary_id as diary_id5_2_3_, foodlist1_.writer_id as writer_i6_2_3_, foodlist1_.diet_id as diet_id7_2_3_, foodlist1_.diary_id as diary_id0_2_3_, foodlist1_.writer_id as writer_i0_2_3_, foodlist1_.diet_id as diet_id0_2_3_, foodlist1_.food_id as food_id1_2_3_, foodlist1_.diary_id as diary_id0_2_0_, foodlist1_.writer_id as writer_i0_2_0_, foodlist1_.diet_id as diet_id0_2_0_, foodlist1_.food_id as food_id1_2_0_, foodlist1_.diary_id as diary_id5_2_0_, foodlist1_.writer_id as writer_i6_2_0_, foodlist1_.diet_id as diet_id7_2_0_, foodlist1_.amount as amount2_2_0_, foodlist1_.amount_unit as amount_u3_2_0_, foodlist1_.food_name as food_nam4_2_0_ from diet diet0_ left outer join food foodlist1_ on diet0_.diary_id=foodlist1_.diary_id and diet0_.writer_id=foodlist1_.writer_id and diet0_.diet_id=foodlist1_.diet_id where diet0_.diary_id=? and diet0_.writer_id=? and diet0_.diet_id=?
2022-05-15 11:14:08.673  INFO 15592 --- [           main] c.d.r.a.service.domain.SaveDiaryService  : saveDietOfWriterById
Hibernate: select diabetesdi0_.diary_id as diary_id1_0_, diabetesdi0_.writer_id as writer_i2_0_, diabetesdi0_.created_at as created_3_0_, diabetesdi0_.updated_at as updated_4_0_, diabetesdi0_.fpg as fpg5_0_, diabetesdi0_.remark as remark6_0_, diabetesdi0_.written_time as written_7_0_ from diabetes_diary diabetesdi0_ where diabetesdi0_.writer_id=? and diabetesdi0_.diary_id=?
Hibernate: insert into diet (blood_sugar, eat_time, diary_id, writer_id, diet_id) values (?, ?, ?, ?, ?)
Hibernate: select max(diet0_.diet_id) as col_0_0_ from diet diet0_
Hibernate: select diet0_.diary_id as diary_id0_1_1_, diet0_.writer_id as writer_i0_1_1_, diet0_.diet_id as diet_id1_1_1_, diet0_.diary_id as diary_id4_1_1_, diet0_.writer_id as writer_i5_1_1_, diet0_.blood_sugar as blood_su2_1_1_, diet0_.eat_time as eat_time3_1_1_, foodlist1_.diary_id as diary_id5_2_3_, foodlist1_.writer_id as writer_i6_2_3_, foodlist1_.diet_id as diet_id7_2_3_, foodlist1_.diary_id as diary_id0_2_3_, foodlist1_.writer_id as writer_i0_2_3_, foodlist1_.diet_id as diet_id0_2_3_, foodlist1_.food_id as food_id1_2_3_, foodlist1_.diary_id as diary_id0_2_0_, foodlist1_.writer_id as writer_i0_2_0_, foodlist1_.diet_id as diet_id0_2_0_, foodlist1_.food_id as food_id1_2_0_, foodlist1_.diary_id as diary_id5_2_0_, foodlist1_.writer_id as writer_i6_2_0_, foodlist1_.diet_id as diet_id7_2_0_, foodlist1_.amount as amount2_2_0_, foodlist1_.amount_unit as amount_u3_2_0_, foodlist1_.food_name as food_nam4_2_0_ from diet diet0_ left outer join food foodlist1_ on diet0_.diary_id=foodlist1_.diary_id and diet0_.writer_id=foodlist1_.writer_id and diet0_.diet_id=foodlist1_.diet_id where diet0_.diary_id=? and diet0_.writer_id=? and diet0_.diet_id=?
2022-05-15 11:14:08.673  INFO 15592 --- [           main] c.d.r.a.service.domain.SaveDiaryService  : saveDietOfWriterById
Hibernate: select diabetesdi0_.diary_id as diary_id1_0_, diabetesdi0_.writer_id as writer_i2_0_, diabetesdi0_.created_at as created_3_0_, diabetesdi0_.updated_at as updated_4_0_, diabetesdi0_.fpg as fpg5_0_, diabetesdi0_.remark as remark6_0_, diabetesdi0_.written_time as written_7_0_ from diabetes_diary diabetesdi0_ where diabetesdi0_.writer_id=? and diabetesdi0_.diary_id=?
Hibernate: insert into diet (blood_sugar, eat_time, diary_id, writer_id, diet_id) values (?, ?, ?, ?, ?)
Hibernate: select max(diet0_.diet_id) as col_0_0_ from diet diet0_
Hibernate: select diet0_.diary_id as diary_id0_1_1_, diet0_.writer_id as writer_i0_1_1_, diet0_.diet_id as diet_id1_1_1_, diet0_.diary_id as diary_id4_1_1_, diet0_.writer_id as writer_i5_1_1_, diet0_.blood_sugar as blood_su2_1_1_, diet0_.eat_time as eat_time3_1_1_, foodlist1_.diary_id as diary_id5_2_3_, foodlist1_.writer_id as writer_i6_2_3_, foodlist1_.diet_id as diet_id7_2_3_, foodlist1_.diary_id as diary_id0_2_3_, foodlist1_.writer_id as writer_i0_2_3_, foodlist1_.diet_id as diet_id0_2_3_, foodlist1_.food_id as food_id1_2_3_, foodlist1_.diary_id as diary_id0_2_0_, foodlist1_.writer_id as writer_i0_2_0_, foodlist1_.diet_id as diet_id0_2_0_, foodlist1_.food_id as food_id1_2_0_, foodlist1_.diary_id as diary_id5_2_0_, foodlist1_.writer_id as writer_i6_2_0_, foodlist1_.diet_id as diet_id7_2_0_, foodlist1_.amount as amount2_2_0_, foodlist1_.amount_unit as amount_u3_2_0_, foodlist1_.food_name as food_nam4_2_0_ from diet diet0_ left outer join food foodlist1_ on diet0_.diary_id=foodlist1_.diary_id and diet0_.writer_id=foodlist1_.writer_id and diet0_.diet_id=foodlist1_.diet_id where diet0_.diary_id=? and diet0_.writer_id=? and diet0_.diet_id=?
2022-05-15 11:14:08.674  INFO 15592 --- [           main] c.d.r.a.service.domain.SaveDiaryService  : saveDietOfWriterById
Hibernate: select diabetesdi0_.diary_id as diary_id1_0_, diabetesdi0_.writer_id as writer_i2_0_, diabetesdi0_.created_at as created_3_0_, diabetesdi0_.updated_at as updated_4_0_, diabetesdi0_.fpg as fpg5_0_, diabetesdi0_.remark as remark6_0_, diabetesdi0_.written_time as written_7_0_ from diabetes_diary diabetesdi0_ where diabetesdi0_.writer_id=? and diabetesdi0_.diary_id=?
Hibernate: insert into diet (blood_sugar, eat_time, diary_id, writer_id, diet_id) values (?, ?, ?, ?, ?)
Hibernate: select max(diet0_.diet_id) as col_0_0_ from diet diet0_
Hibernate: select diet0_.diary_id as diary_id0_1_1_, diet0_.writer_id as writer_i0_1_1_, diet0_.diet_id as diet_id1_1_1_, diet0_.diary_id as diary_id4_1_1_, diet0_.writer_id as writer_i5_1_1_, diet0_.blood_sugar as blood_su2_1_1_, diet0_.eat_time as eat_time3_1_1_, foodlist1_.diary_id as diary_id5_2_3_, foodlist1_.writer_id as writer_i6_2_3_, foodlist1_.diet_id as diet_id7_2_3_, foodlist1_.diary_id as diary_id0_2_3_, foodlist1_.writer_id as writer_i0_2_3_, foodlist1_.diet_id as diet_id0_2_3_, foodlist1_.food_id as food_id1_2_3_, foodlist1_.diary_id as diary_id0_2_0_, foodlist1_.writer_id as writer_i0_2_0_, foodlist1_.diet_id as diet_id0_2_0_, foodlist1_.food_id as food_id1_2_0_, foodlist1_.diary_id as diary_id5_2_0_, foodlist1_.writer_id as writer_i6_2_0_, foodlist1_.diet_id as diet_id7_2_0_, foodlist1_.amount as amount2_2_0_, foodlist1_.amount_unit as amount_u3_2_0_, foodlist1_.food_name as food_nam4_2_0_ from diet diet0_ left outer join food foodlist1_ on diet0_.diary_id=foodlist1_.diary_id and diet0_.writer_id=foodlist1_.writer_id and diet0_.diet_id=foodlist1_.diet_id where diet0_.diary_id=? and diet0_.writer_id=? and diet0_.diet_id=?
2022-05-15 11:14:08.675  INFO 15592 --- [           main] c.d.r.a.service.domain.SaveDiaryService  : saveDietOfWriterById
Hibernate: select diabetesdi0_.diary_id as diary_id1_0_, diabetesdi0_.writer_id as writer_i2_0_, diabetesdi0_.created_at as created_3_0_, diabetesdi0_.updated_at as updated_4_0_, diabetesdi0_.fpg as fpg5_0_, diabetesdi0_.remark as remark6_0_, diabetesdi0_.written_time as written_7_0_ from diabetes_diary diabetesdi0_ where diabetesdi0_.writer_id=? and diabetesdi0_.diary_id=?
Hibernate: insert into diet (blood_sugar, eat_time, diary_id, writer_id, diet_id) values (?, ?, ?, ?, ?)
Hibernate: select max(diet0_.diet_id) as col_0_0_ from diet diet0_
Hibernate: select diet0_.diary_id as diary_id0_1_1_, diet0_.writer_id as writer_i0_1_1_, diet0_.diet_id as diet_id1_1_1_, diet0_.diary_id as diary_id4_1_1_, diet0_.writer_id as writer_i5_1_1_, diet0_.blood_sugar as blood_su2_1_1_, diet0_.eat_time as eat_time3_1_1_, foodlist1_.diary_id as diary_id5_2_3_, foodlist1_.writer_id as writer_i6_2_3_, foodlist1_.diet_id as diet_id7_2_3_, foodlist1_.diary_id as diary_id0_2_3_, foodlist1_.writer_id as writer_i0_2_3_, foodlist1_.diet_id as diet_id0_2_3_, foodlist1_.food_id as food_id1_2_3_, foodlist1_.diary_id as diary_id0_2_0_, foodlist1_.writer_id as writer_i0_2_0_, foodlist1_.diet_id as diet_id0_2_0_, foodlist1_.food_id as food_id1_2_0_, foodlist1_.diary_id as diary_id5_2_0_, foodlist1_.writer_id as writer_i6_2_0_, foodlist1_.diet_id as diet_id7_2_0_, foodlist1_.amount as amount2_2_0_, foodlist1_.amount_unit as amount_u3_2_0_, foodlist1_.food_name as food_nam4_2_0_ from diet diet0_ left outer join food foodlist1_ on diet0_.diary_id=foodlist1_.diary_id and diet0_.writer_id=foodlist1_.writer_id and diet0_.diet_id=foodlist1_.diet_id where diet0_.diary_id=? and diet0_.writer_id=? and diet0_.diet_id=?
2022-05-15 11:14:08.676  INFO 15592 --- [           main] c.d.r.a.service.domain.SaveDiaryService  : saveDietOfWriterById
Hibernate: select diabetesdi0_.diary_id as diary_id1_0_, diabetesdi0_.writer_id as writer_i2_0_, diabetesdi0_.created_at as created_3_0_, diabetesdi0_.updated_at as updated_4_0_, diabetesdi0_.fpg as fpg5_0_, diabetesdi0_.remark as remark6_0_, diabetesdi0_.written_time as written_7_0_ from diabetes_diary diabetesdi0_ where diabetesdi0_.writer_id=? and diabetesdi0_.diary_id=?
Hibernate: insert into diet (blood_sugar, eat_time, diary_id, writer_id, diet_id) values (?, ?, ?, ?, ?)
Hibernate: select max(diet0_.diet_id) as col_0_0_ from diet diet0_
Hibernate: select diet0_.diary_id as diary_id0_1_1_, diet0_.writer_id as writer_i0_1_1_, diet0_.diet_id as diet_id1_1_1_, diet0_.diary_id as diary_id4_1_1_, diet0_.writer_id as writer_i5_1_1_, diet0_.blood_sugar as blood_su2_1_1_, diet0_.eat_time as eat_time3_1_1_, foodlist1_.diary_id as diary_id5_2_3_, foodlist1_.writer_id as writer_i6_2_3_, foodlist1_.diet_id as diet_id7_2_3_, foodlist1_.diary_id as diary_id0_2_3_, foodlist1_.writer_id as writer_i0_2_3_, foodlist1_.diet_id as diet_id0_2_3_, foodlist1_.food_id as food_id1_2_3_, foodlist1_.diary_id as diary_id0_2_0_, foodlist1_.writer_id as writer_i0_2_0_, foodlist1_.diet_id as diet_id0_2_0_, foodlist1_.food_id as food_id1_2_0_, foodlist1_.diary_id as diary_id5_2_0_, foodlist1_.writer_id as writer_i6_2_0_, foodlist1_.diet_id as diet_id7_2_0_, foodlist1_.amount as amount2_2_0_, foodlist1_.amount_unit as amount_u3_2_0_, foodlist1_.food_name as food_nam4_2_0_ from diet diet0_ left outer join food foodlist1_ on diet0_.diary_id=foodlist1_.diary_id and diet0_.writer_id=foodlist1_.writer_id and diet0_.diet_id=foodlist1_.diet_id where diet0_.diary_id=? and diet0_.writer_id=? and diet0_.diet_id=?

1:n save 시 추가적인 update 쿼리가 발생하지 않는다.

일지 작성 코드와 로그

    @Transactional
    public Long postDiaryWithEntities(PrincipalDetails principalDetails, SecurityDiaryPostRequestDTO dto) {
        logger.info("post diary in service logic");
        checkNotNull(principalDetails, "principalDetails must be provided");

        /* 0. 현재 세션에 담긴 사용자 정보 판별 */
        Writer writer = writerRepository.findById(principalDetails.getWriter().getId()).orElseThrow(() -> new NoResultException("작성자가 없습니다."));

        /* 2-1. LocalDateTime JSON 직렬화 */
        LocalDateTime writtenTime = convertStringToLocalDateTime(dto);

        /* 2-2. 혈당 일지 저장 */
        DiabetesDiary diary = new DiabetesDiary(getNextIdOfDiary(), writer, dto.getFastingPlasmaGlucose(), dto.getRemark(), writtenTime);
        writer.addDiary(diary);

        /* 3. 아침 점심 저녁 식사 저장 */

        Diet breakFast = new Diet(getNextIdOfDiet(), diary, EatTime.BreakFast, dto.getBreakFastSugar());
        diary.addDiet(breakFast);

        Diet lunch = new Diet(getNextIdOfDiet(), diary, EatTime.Lunch, dto.getLunchSugar());
        diary.addDiet(lunch);

        Diet dinner = new Diet(getNextIdOfDiet(), diary, EatTime.Dinner, dto.getDinnerSugar());
        diary.addDiet(dinner);

        /* 4-1. 아침 음식 저장 */
        dto.getBreakFastFoods().forEach(elem -> {
            Food food = new Food(getNextIdOfFood(), breakFast, elem.getFoodName(), elem.getAmount(), elem.getAmountUnit());
            breakFast.addFood(food);
        });

        /* 4-2. 점심 음식 저장 */
        dto.getLunchFoods().forEach(elem -> {
            Food food = new Food(getNextIdOfFood(), lunch, elem.getFoodName(), elem.getAmount(), elem.getAmountUnit());
            lunch.addFood(food);
        });

        /* 4-3. 저녁 음식 저장 */
        dto.getDinnerFoods().forEach(elem -> {
            Food food = new Food(getNextIdOfFood(), dinner, elem.getFoodName(), elem.getAmount(), elem.getAmountUnit());
            dinner.addFood(food);
        });

        writerRepository.save(writer);

        return diary.getId();
    }

상황은 일지, 식단 3개, 음식 4개이다.

2022-05-15 10:59:05.448  INFO 11288 --- [nio-8084-exec-8] .d.r.a.c.s.d.SecurityDiaryRestController : post diary with authenticated user
2022-05-15 10:59:05.451  INFO 11288 --- [nio-8084-exec-8] c.d.r.a.service.domain.SaveDiaryService  : post diary in service logic
Hibernate: select writer0_.writer_id as writer_i1_4_0_, writer0_.created_at as created_2_4_0_, writer0_.updated_at as updated_3_4_0_, writer0_.email as email4_4_0_, writer0_.name as name5_4_0_, writer0_.password as password6_4_0_, writer0_.profile_id as profile10_4_0_, writer0_.provider as provider7_4_0_, writer0_.provider_id as provider8_4_0_, writer0_.role as role9_4_0_, profile1_.profile_id as profile_1_3_1_, profile1_.diabetes_phase as diabetes2_3_1_ from writer writer0_ left outer join profile profile1_ on writer0_.profile_id=profile1_.profile_id where writer0_.writer_id=?
Hibernate: select max(diabetesdi0_.diary_id) as col_0_0_ from diabetes_diary diabetesdi0_
Hibernate: select diaries0_.writer_id as writer_i2_0_0_, diaries0_.diary_id as diary_id1_0_0_, diaries0_.diary_id as diary_id1_0_1_, diaries0_.writer_id as writer_i2_0_1_, diaries0_.created_at as created_3_0_1_, diaries0_.updated_at as updated_4_0_1_, diaries0_.fpg as fpg5_0_1_, diaries0_.remark as remark6_0_1_, diaries0_.written_time as written_7_0_1_ from diabetes_diary diaries0_ where diaries0_.writer_id=?
Hibernate: insert into diabetes_diary (created_at, updated_at, fpg, remark, written_time, diary_id, writer_id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: select max(diet0_.diet_id) as col_0_0_ from diet diet0_
Hibernate: insert into diet (blood_sugar, eat_time, diary_id, writer_id, diet_id) values (?, ?, ?, ?, ?)
Hibernate: select max(diet0_.diet_id) as col_0_0_ from diet diet0_
Hibernate: insert into diet (blood_sugar, eat_time, diary_id, writer_id, diet_id) values (?, ?, ?, ?, ?)
Hibernate: select max(diet0_.diet_id) as col_0_0_ from diet diet0_
Hibernate: insert into diet (blood_sugar, eat_time, diary_id, writer_id, diet_id) values (?, ?, ?, ?, ?)
Hibernate: select max(food0_.food_id) as col_0_0_ from food food0_
Hibernate: insert into food (amount, amount_unit, food_name, diary_id, writer_id, diet_id, food_id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: select max(food0_.food_id) as col_0_0_ from food food0_
Hibernate: insert into food (amount, amount_unit, food_name, diary_id, writer_id, diet_id, food_id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: select max(food0_.food_id) as col_0_0_ from food food0_
Hibernate: insert into food (amount, amount_unit, food_name, diary_id, writer_id, diet_id, food_id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: select max(food0_.food_id) as col_0_0_ from food food0_
Hibernate: select food0_.diary_id as diary_id0_2_0_, food0_.writer_id as writer_i0_2_0_, food0_.diet_id as diet_id0_2_0_, food0_.food_id as food_id1_2_0_, food0_.diary_id as diary_id5_2_0_, food0_.writer_id as writer_i6_2_0_, food0_.diet_id as diet_id7_2_0_, food0_.amount as amount2_2_0_, food0_.amount_unit as amount_u3_2_0_, food0_.food_name as food_nam4_2_0_ from food food0_ where food0_.diary_id=? and food0_.writer_id=? and food0_.diet_id=? and food0_.food_id=?
Hibernate: insert into food (amount, amount_unit, food_name, diary_id, writer_id, diet_id, food_id) values (?, ?, ?, ?, ?, ?, ?)

일지 수정 코드와 로그

   @Transactional
    public Long updateDiaryWithEntities(PrincipalDetails principalDetails, SecurityDiaryUpdateDTO dto) {
        logger.info("update diary in service logic");
        checkNotNull(principalDetails, "principalDetails must be provided");

        /* 0. 현재 세션에 담긴 사용자 정보 판별 */
        Writer writer = writerRepository.findById(principalDetails.getWriter().getId()).orElseThrow(() -> new NoResultException("작성자가 없습니다."));

        /* 1. 혈당 일지 변경 감지 되었으면 수정. */
        Long diabetesDiaryId = dto.getDiaryId();
        if (dto.isDiaryDirty()) {
            DiabetesDiary targetDiary = diaryRepository.findOneDiabetesDiaryByIdInWriter(principalDetails.getWriter().getId(), diabetesDiaryId)
                    .orElseThrow(() -> new NoResultException("해당 혈당일지가 존재하지 않습니다."));

            targetDiary.update(dto.getFastingPlasmaGlucose(), dto.getRemark());
        }

        /* 2. 아침 식단 변경 감지 되었으면 수정 */
        Long breakFastId = dto.getBreakFastId();

        Diet targetBreakFast = dietRepository.findOneDietByIdInDiary(principalDetails.getWriter().getId(), diabetesDiaryId, breakFastId)
                .orElseThrow(() -> new NoResultException("해당 아침 식단이 존재하지 않습니다."));
        if (dto.isBreakFastDirty()) {
            targetBreakFast.update(EatTime.BreakFast, dto.getBreakFastSugar());
        }

        /* 3. 점심 식단 변경 감지 되었으면 수정 */
        Long lunchId = dto.getLunchId();

        Diet targetLunch = dietRepository.findOneDietByIdInDiary(principalDetails.getWriter().getId(), diabetesDiaryId, lunchId)
                .orElseThrow(() -> new NoResultException("해당 점심 식단이 존재하지 않습니다."));
        if (dto.isLunchDirty()) {
            targetLunch.update(EatTime.Lunch, dto.getLunchSugar());
        }

        /* 4. 저녁 식단 변경 감지 되었으면 수정 */
        Long dinnerId = dto.getDinnerId();

        Diet targetDinner = dietRepository.findOneDietByIdInDiary(principalDetails.getWriter().getId(), diabetesDiaryId, dinnerId)
                .orElseThrow(() -> new NoResultException("해당 저녁 식단이 존재하지 않습니다."));

        if (dto.isDinnerDirty()) {
            targetDinner.update(EatTime.Dinner, dto.getDinnerSugar());
        }

        /* 5. 기존 음식 엔티티 삭제 ( in (id) 벌크 삭제) */
        List<SecurityFoodForUpdateDTO> allOldFoods = new ArrayList<>();
        allOldFoods.addAll(dto.getOldBreakFastFoods());
        allOldFoods.addAll(dto.getOldLunchFoods());
        allOldFoods.addAll(dto.getOldDinnerFoods());

        if (allOldFoods.size() > 0) {
            List<EntityId<Food, Long>> foodEntityIds = allOldFoods.stream().map(elem -> EntityId.of(Food.class, elem.getId())).collect(Collectors.toList());
            bulkDeleteFoods(foodEntityIds);
        }

        /* 6. 음식 엔티티 새로이 생성 */
        dto.getNewBreakFastFoods()
                .forEach(elem -> {
                    Food food = new Food(saveDiaryService.getNextIdOfFood(), targetBreakFast, elem.getFoodName(), elem.getAmount(), elem.getAmountUnit());
                    targetBreakFast.addFood(food);
                });

        dto.getNewLunchFoods()
                .forEach(elem -> {
                    Food food = new Food(saveDiaryService.getNextIdOfFood(), targetLunch, elem.getFoodName(), elem.getAmount(), elem.getAmountUnit());
                    targetLunch.addFood(food);
                });

        dto.getNewDinnerFoods()
                .forEach(elem -> {
                    Food food = new Food(saveDiaryService.getNextIdOfFood(), targetDinner, elem.getFoodName(), elem.getAmount(), elem.getAmountUnit());
                    targetDinner.addFood(food);
                });

        writerRepository.save(writer);

        return diabetesDiaryId;
    }

위 생성된 일지에서 일지 수정, 식단 3개 수정, 음식 4개 변경된 상황이다.

2022-05-15 11:02:55.982  INFO 11288 --- [nio-8084-exec-1] .d.r.a.c.s.d.SecurityDiaryRestController : update diabetes diary from browser
2022-05-15 11:02:55.982  INFO 11288 --- [nio-8084-exec-1] c.d.r.a.s.d.UpdateDeleteDiaryService     : update diary in service logic
Hibernate: select writer0_.writer_id as writer_i1_4_0_, writer0_.created_at as created_2_4_0_, writer0_.updated_at as updated_3_4_0_, writer0_.email as email4_4_0_, writer0_.name as name5_4_0_, writer0_.password as password6_4_0_, writer0_.profile_id as profile10_4_0_, writer0_.provider as provider7_4_0_, writer0_.provider_id as provider8_4_0_, writer0_.role as role9_4_0_, profile1_.profile_id as profile_1_3_1_, profile1_.diabetes_phase as diabetes2_3_1_ from writer writer0_ left outer join profile profile1_ on writer0_.profile_id=profile1_.profile_id where writer0_.writer_id=?
Hibernate: select diabetesdi0_.diary_id as diary_id1_0_, diabetesdi0_.writer_id as writer_i2_0_, diabetesdi0_.created_at as created_3_0_, diabetesdi0_.updated_at as updated_4_0_, diabetesdi0_.fpg as fpg5_0_, diabetesdi0_.remark as remark6_0_, diabetesdi0_.written_time as written_7_0_ from diabetes_diary diabetesdi0_ where diabetesdi0_.writer_id=? and diabetesdi0_.diary_id=?
Hibernate: update diabetes_diary set created_at=?, updated_at=?, fpg=?, remark=?, written_time=? where diary_id=? and writer_id=?
Hibernate: select diet0_.diary_id as diary_id0_1_, diet0_.writer_id as writer_i0_1_, diet0_.diet_id as diet_id1_1_, diet0_.diary_id as diary_id4_1_, diet0_.writer_id as writer_i5_1_, diet0_.blood_sugar as blood_su2_1_, diet0_.eat_time as eat_time3_1_ from diet diet0_ inner join diabetes_diary diabetesdi1_ on diet0_.diary_id=diabetesdi1_.diary_id and diet0_.writer_id=diabetesdi1_.writer_id and (diabetesdi1_.writer_id=? and diabetesdi1_.diary_id=?) where diet0_.diet_id=?
Hibernate: update diet set blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: select diet0_.diary_id as diary_id0_1_, diet0_.writer_id as writer_i0_1_, diet0_.diet_id as diet_id1_1_, diet0_.diary_id as diary_id4_1_, diet0_.writer_id as writer_i5_1_, diet0_.blood_sugar as blood_su2_1_, diet0_.eat_time as eat_time3_1_ from diet diet0_ inner join diabetes_diary diabetesdi1_ on diet0_.diary_id=diabetesdi1_.diary_id and diet0_.writer_id=diabetesdi1_.writer_id and (diabetesdi1_.writer_id=? and diabetesdi1_.diary_id=?) where diet0_.diet_id=?
Hibernate: update diet set blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: select diet0_.diary_id as diary_id0_1_, diet0_.writer_id as writer_i0_1_, diet0_.diet_id as diet_id1_1_, diet0_.diary_id as diary_id4_1_, diet0_.writer_id as writer_i5_1_, diet0_.blood_sugar as blood_su2_1_, diet0_.eat_time as eat_time3_1_ from diet diet0_ inner join diabetes_diary diabetesdi1_ on diet0_.diary_id=diabetesdi1_.diary_id and diet0_.writer_id=diabetesdi1_.writer_id and (diabetesdi1_.writer_id=? and diabetesdi1_.diary_id=?) where diet0_.diet_id=?
2022-05-15 11:02:56.004  INFO 11288 --- [nio-8084-exec-1] c.d.r.a.s.d.UpdateDeleteDiaryService     : bulk delete food service
Hibernate: delete from food where food_id in (? , ? , ? , ?)
Hibernate: select max(food0_.food_id) as col_0_0_ from food food0_
Hibernate: select foodlist0_.diary_id as diary_id5_2_0_, foodlist0_.writer_id as writer_i6_2_0_, foodlist0_.diet_id as diet_id7_2_0_, foodlist0_.diary_id as diary_id0_2_0_, foodlist0_.writer_id as writer_i0_2_0_, foodlist0_.diet_id as diet_id0_2_0_, foodlist0_.food_id as food_id1_2_0_, foodlist0_.diary_id as diary_id0_2_1_, foodlist0_.writer_id as writer_i0_2_1_, foodlist0_.diet_id as diet_id0_2_1_, foodlist0_.food_id as food_id1_2_1_, foodlist0_.diary_id as diary_id5_2_1_, foodlist0_.writer_id as writer_i6_2_1_, foodlist0_.diet_id as diet_id7_2_1_, foodlist0_.amount as amount2_2_1_, foodlist0_.amount_unit as amount_u3_2_1_, foodlist0_.food_name as food_nam4_2_1_ from food foodlist0_ where foodlist0_.diary_id=? and foodlist0_.writer_id=? and foodlist0_.diet_id=?
Hibernate: insert into food (amount, amount_unit, food_name, diary_id, writer_id, diet_id, food_id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: update diet set blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: select max(food0_.food_id) as col_0_0_ from food food0_
Hibernate: select foodlist0_.diary_id as diary_id5_2_0_, foodlist0_.writer_id as writer_i6_2_0_, foodlist0_.diet_id as diet_id7_2_0_, foodlist0_.diary_id as diary_id0_2_0_, foodlist0_.writer_id as writer_i0_2_0_, foodlist0_.diet_id as diet_id0_2_0_, foodlist0_.food_id as food_id1_2_0_, foodlist0_.diary_id as diary_id0_2_1_, foodlist0_.writer_id as writer_i0_2_1_, foodlist0_.diet_id as diet_id0_2_1_, foodlist0_.food_id as food_id1_2_1_, foodlist0_.diary_id as diary_id5_2_1_, foodlist0_.writer_id as writer_i6_2_1_, foodlist0_.diet_id as diet_id7_2_1_, foodlist0_.amount as amount2_2_1_, foodlist0_.amount_unit as amount_u3_2_1_, foodlist0_.food_name as food_nam4_2_1_ from food foodlist0_ where foodlist0_.diary_id=? and foodlist0_.writer_id=? and foodlist0_.diet_id=?
Hibernate: insert into food (amount, amount_unit, food_name, diary_id, writer_id, diet_id, food_id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: select max(food0_.food_id) as col_0_0_ from food food0_
Hibernate: select foodlist0_.diary_id as diary_id5_2_0_, foodlist0_.writer_id as writer_i6_2_0_, foodlist0_.diet_id as diet_id7_2_0_, foodlist0_.diary_id as diary_id0_2_0_, foodlist0_.writer_id as writer_i0_2_0_, foodlist0_.diet_id as diet_id0_2_0_, foodlist0_.food_id as food_id1_2_0_, foodlist0_.diary_id as diary_id0_2_1_, foodlist0_.writer_id as writer_i0_2_1_, foodlist0_.diet_id as diet_id0_2_1_, foodlist0_.food_id as food_id1_2_1_, foodlist0_.diary_id as diary_id5_2_1_, foodlist0_.writer_id as writer_i6_2_1_, foodlist0_.diet_id as diet_id7_2_1_, foodlist0_.amount as amount2_2_1_, foodlist0_.amount_unit as amount_u3_2_1_, foodlist0_.food_name as food_nam4_2_1_ from food foodlist0_ where foodlist0_.diary_id=? and foodlist0_.writer_id=? and foodlist0_.diet_id=?
Hibernate: insert into food (amount, amount_unit, food_name, diary_id, writer_id, diet_id, food_id) values (?, ?, ?, ?, ?, ?, ?)

새로운 의문점

작성자와 일지에 대해서만 BaseTimeEntity를 상속했더니 update 쿼리가 발생하지 않았다. 그러면 음식과 식단에도 다 상속시키면 해당 문제가 발생할까가 궁금해졌다.

일지 작성 로그

음식, 일지에도 작성 시간, 갱신 시간을 넣어보았다.
일지, 식단 3개, 음식 3개에 대한 시나리오인데, update 쿼리가 추가적으로 발생하지 않는다.

2022-05-15 11:59:21.245  INFO 9092 --- [nio-8084-exec-2] .d.r.a.c.s.d.SecurityDiaryRestController : post diary with authenticated user
2022-05-15 11:59:21.248  INFO 9092 --- [nio-8084-exec-2] c.d.r.a.service.domain.SaveDiaryService  : post diary in service logic
Hibernate: select writer0_.writer_id as writer_i1_4_0_, writer0_.created_at as created_2_4_0_, writer0_.updated_at as updated_3_4_0_, writer0_.email as email4_4_0_, writer0_.name as name5_4_0_, writer0_.password as password6_4_0_, writer0_.profile_id as profile10_4_0_, writer0_.provider as provider7_4_0_, writer0_.provider_id as provider8_4_0_, writer0_.role as role9_4_0_, profile1_.profile_id as profile_1_3_1_, profile1_.diabetes_phase as diabetes2_3_1_ from writer writer0_ left outer join profile profile1_ on writer0_.profile_id=profile1_.profile_id where writer0_.writer_id=?
Hibernate: select max(diabetesdi0_.diary_id) as col_0_0_ from diabetes_diary diabetesdi0_
Hibernate: select diaries0_.writer_id as writer_i2_0_0_, diaries0_.diary_id as diary_id1_0_0_, diaries0_.diary_id as diary_id1_0_1_, diaries0_.writer_id as writer_i2_0_1_, diaries0_.created_at as created_3_0_1_, diaries0_.updated_at as updated_4_0_1_, diaries0_.fpg as fpg5_0_1_, diaries0_.remark as remark6_0_1_, diaries0_.written_time as written_7_0_1_ from diabetes_diary diaries0_ where diaries0_.writer_id=?
Hibernate: insert into diabetes_diary (created_at, updated_at, fpg, remark, written_time, diary_id, writer_id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: select max(diet0_.diet_id) as col_0_0_ from diet diet0_
Hibernate: insert into diet (created_at, updated_at, blood_sugar, eat_time, diary_id, writer_id, diet_id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: select max(diet0_.diet_id) as col_0_0_ from diet diet0_
Hibernate: insert into diet (created_at, updated_at, blood_sugar, eat_time, diary_id, writer_id, diet_id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: select max(diet0_.diet_id) as col_0_0_ from diet diet0_
Hibernate: insert into diet (created_at, updated_at, blood_sugar, eat_time, diary_id, writer_id, diet_id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: select max(food0_.food_id) as col_0_0_ from food food0_
Hibernate: insert into food (created_at, updated_at, amount, amount_unit, food_name, diary_id, writer_id, diet_id, food_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: select max(food0_.food_id) as col_0_0_ from food food0_
Hibernate: insert into food (created_at, updated_at, amount, amount_unit, food_name, diary_id, writer_id, diet_id, food_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: select max(food0_.food_id) as col_0_0_ from food food0_
Hibernate: select food0_.diary_id as diary_id0_2_0_, food0_.writer_id as writer_i0_2_0_, food0_.diet_id as diet_id0_2_0_, food0_.food_id as food_id1_2_0_, food0_.diary_id as diary_id7_2_0_, food0_.writer_id as writer_i8_2_0_, food0_.diet_id as diet_id9_2_0_, food0_.created_at as created_2_2_0_, food0_.updated_at as updated_3_2_0_, food0_.amount as amount4_2_0_, food0_.amount_unit as amount_u5_2_0_, food0_.food_name as food_nam6_2_0_ from food food0_ where food0_.diary_id=? and food0_.writer_id=? and food0_.diet_id=? and food0_.food_id=?
Hibernate: insert into food (created_at, updated_at, amount, amount_unit, food_name, diary_id, writer_id, diet_id, food_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?)

일지 수정 로그

일지 , 식단 3개, 음식 3개 변경에 대한 시나리오다. 이 역시 update 쿼리가 추가적으로 발생하지 않는다??

2022-05-15 12:02:50.637  INFO 9092 --- [nio-8084-exec-9] .d.r.a.c.s.d.SecurityDiaryRestController : update diabetes diary from browser
2022-05-15 12:02:50.640  INFO 9092 --- [nio-8084-exec-9] c.d.r.a.s.d.UpdateDeleteDiaryService     : update diary in service logic
Hibernate: select writer0_.writer_id as writer_i1_4_0_, writer0_.created_at as created_2_4_0_, writer0_.updated_at as updated_3_4_0_, writer0_.email as email4_4_0_, writer0_.name as name5_4_0_, writer0_.password as password6_4_0_, writer0_.profile_id as profile10_4_0_, writer0_.provider as provider7_4_0_, writer0_.provider_id as provider8_4_0_, writer0_.role as role9_4_0_, profile1_.profile_id as profile_1_3_1_, profile1_.diabetes_phase as diabetes2_3_1_ from writer writer0_ left outer join profile profile1_ on writer0_.profile_id=profile1_.profile_id where writer0_.writer_id=?
Hibernate: select diabetesdi0_.diary_id as diary_id1_0_, diabetesdi0_.writer_id as writer_i2_0_, diabetesdi0_.created_at as created_3_0_, diabetesdi0_.updated_at as updated_4_0_, diabetesdi0_.fpg as fpg5_0_, diabetesdi0_.remark as remark6_0_, diabetesdi0_.written_time as written_7_0_ from diabetes_diary diabetesdi0_ where diabetesdi0_.writer_id=? and diabetesdi0_.diary_id=?
Hibernate: update diabetes_diary set created_at=?, updated_at=?, fpg=?, remark=?, written_time=? where diary_id=? and writer_id=?
Hibernate: select diet0_.diary_id as diary_id0_1_, diet0_.writer_id as writer_i0_1_, diet0_.diet_id as diet_id1_1_, diet0_.diary_id as diary_id6_1_, diet0_.writer_id as writer_i7_1_, diet0_.created_at as created_2_1_, diet0_.updated_at as updated_3_1_, diet0_.blood_sugar as blood_su4_1_, diet0_.eat_time as eat_time5_1_ from diet diet0_ inner join diabetes_diary diabetesdi1_ on diet0_.diary_id=diabetesdi1_.diary_id and diet0_.writer_id=diabetesdi1_.writer_id and (diabetesdi1_.writer_id=? and diabetesdi1_.diary_id=?) where diet0_.diet_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: select diet0_.diary_id as diary_id0_1_, diet0_.writer_id as writer_i0_1_, diet0_.diet_id as diet_id1_1_, diet0_.diary_id as diary_id6_1_, diet0_.writer_id as writer_i7_1_, diet0_.created_at as created_2_1_, diet0_.updated_at as updated_3_1_, diet0_.blood_sugar as blood_su4_1_, diet0_.eat_time as eat_time5_1_ from diet diet0_ inner join diabetes_diary diabetesdi1_ on diet0_.diary_id=diabetesdi1_.diary_id and diet0_.writer_id=diabetesdi1_.writer_id and (diabetesdi1_.writer_id=? and diabetesdi1_.diary_id=?) where diet0_.diet_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: select diet0_.diary_id as diary_id0_1_, diet0_.writer_id as writer_i0_1_, diet0_.diet_id as diet_id1_1_, diet0_.diary_id as diary_id6_1_, diet0_.writer_id as writer_i7_1_, diet0_.created_at as created_2_1_, diet0_.updated_at as updated_3_1_, diet0_.blood_sugar as blood_su4_1_, diet0_.eat_time as eat_time5_1_ from diet diet0_ inner join diabetes_diary diabetesdi1_ on diet0_.diary_id=diabetesdi1_.diary_id and diet0_.writer_id=diabetesdi1_.writer_id and (diabetesdi1_.writer_id=? and diabetesdi1_.diary_id=?) where diet0_.diet_id=?
2022-05-15 12:02:50.661  INFO 9092 --- [nio-8084-exec-9] c.d.r.a.s.d.UpdateDeleteDiaryService     : bulk delete food service
Hibernate: delete from food where food_id in (? , ? , ?)
Hibernate: select max(food0_.food_id) as col_0_0_ from food food0_
Hibernate: select foodlist0_.diary_id as diary_id7_2_0_, foodlist0_.writer_id as writer_i8_2_0_, foodlist0_.diet_id as diet_id9_2_0_, foodlist0_.diary_id as diary_id0_2_0_, foodlist0_.writer_id as writer_i0_2_0_, foodlist0_.diet_id as diet_id0_2_0_, foodlist0_.food_id as food_id1_2_0_, foodlist0_.diary_id as diary_id0_2_1_, foodlist0_.writer_id as writer_i0_2_1_, foodlist0_.diet_id as diet_id0_2_1_, foodlist0_.food_id as food_id1_2_1_, foodlist0_.diary_id as diary_id7_2_1_, foodlist0_.writer_id as writer_i8_2_1_, foodlist0_.diet_id as diet_id9_2_1_, foodlist0_.created_at as created_2_2_1_, foodlist0_.updated_at as updated_3_2_1_, foodlist0_.amount as amount4_2_1_, foodlist0_.amount_unit as amount_u5_2_1_, foodlist0_.food_name as food_nam6_2_1_ from food foodlist0_ where foodlist0_.diary_id=? and foodlist0_.writer_id=? and foodlist0_.diet_id=?
Hibernate: insert into food (created_at, updated_at, amount, amount_unit, food_name, diary_id, writer_id, diet_id, food_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: select max(food0_.food_id) as col_0_0_ from food food0_
Hibernate: select foodlist0_.diary_id as diary_id7_2_0_, foodlist0_.writer_id as writer_i8_2_0_, foodlist0_.diet_id as diet_id9_2_0_, foodlist0_.diary_id as diary_id0_2_0_, foodlist0_.writer_id as writer_i0_2_0_, foodlist0_.diet_id as diet_id0_2_0_, foodlist0_.food_id as food_id1_2_0_, foodlist0_.diary_id as diary_id0_2_1_, foodlist0_.writer_id as writer_i0_2_1_, foodlist0_.diet_id as diet_id0_2_1_, foodlist0_.food_id as food_id1_2_1_, foodlist0_.diary_id as diary_id7_2_1_, foodlist0_.writer_id as writer_i8_2_1_, foodlist0_.diet_id as diet_id9_2_1_, foodlist0_.created_at as created_2_2_1_, foodlist0_.updated_at as updated_3_2_1_, foodlist0_.amount as amount4_2_1_, foodlist0_.amount_unit as amount_u5_2_1_, foodlist0_.food_name as food_nam6_2_1_ from food foodlist0_ where foodlist0_.diary_id=? and foodlist0_.writer_id=? and foodlist0_.diet_id=?
Hibernate: insert into food (created_at, updated_at, amount, amount_unit, food_name, diary_id, writer_id, diet_id, food_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: select max(food0_.food_id) as col_0_0_ from food food0_
Hibernate: select foodlist0_.diary_id as diary_id7_2_0_, foodlist0_.writer_id as writer_i8_2_0_, foodlist0_.diet_id as diet_id9_2_0_, foodlist0_.diary_id as diary_id0_2_0_, foodlist0_.writer_id as writer_i0_2_0_, foodlist0_.diet_id as diet_id0_2_0_, foodlist0_.food_id as food_id1_2_0_, foodlist0_.diary_id as diary_id0_2_1_, foodlist0_.writer_id as writer_i0_2_1_, foodlist0_.diet_id as diet_id0_2_1_, foodlist0_.food_id as food_id1_2_1_, foodlist0_.diary_id as diary_id7_2_1_, foodlist0_.writer_id as writer_i8_2_1_, foodlist0_.diet_id as diet_id9_2_1_, foodlist0_.created_at as created_2_2_1_, foodlist0_.updated_at as updated_3_2_1_, foodlist0_.amount as amount4_2_1_, foodlist0_.amount_unit as amount_u5_2_1_, foodlist0_.food_name as food_nam6_2_1_ from food foodlist0_ where foodlist0_.diary_id=? and foodlist0_.writer_id=? and foodlist0_.diet_id=?
Hibernate: insert into food (created_at, updated_at, amount, amount_unit, food_name, diary_id, writer_id, diet_id, food_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?)

이전과 동일한 환경으로 만들어서 실험하기

이전 포스트의 save() 메서드들

 @Transactional
    public DiabetesDiary saveDiary(Writer writer, int fastingPlasmaGlucose, String remark, LocalDateTime writtenTime) {
        logger.info("saveDiary");
        DiabetesDiary diary = new DiabetesDiary(getNextIdOfDiary(), writer, fastingPlasmaGlucose, remark, writtenTime);
        writer.addDiary(diary);
        writerRepository.save(writer);
        return diary;
    }

    @Transactional
    public Diet saveDiet(Writer writer, DiabetesDiary diary, EatTime eatTime, int bloodSugar) {
        logger.info("saveDiet");
        Diet diet = new Diet(getNextIdOfDiet(), diary, eatTime, bloodSugar);
        diary.addDiet(diet);
        writerRepository.save(writer);
        return diet;
    }

    @Transactional
    public Food saveFood(Writer writer, Diet diet, String foodName) {
        logger.info("saveFood");
        Food food = new Food(getNextIdOfFood(), diet, foodName);
        diet.addFood(food);
        writerRepository.save(writer);
        return food;
    }

테스트 코드

 @Transactional
    @Test
    public void test() {
        Writer a = saveDiaryService.saveWriter("me", "ME@NAVER.COM", Role.User);
        DiabetesDiary b = saveDiaryService.saveDiary(a, 20, "test1", LocalDateTime.of(2021, 12, 1, 0, 0, 0));
        DiabetesDiary c = saveDiaryService.saveDiary(a, 20, "test1", LocalDateTime.of(2021, 12, 10, 0, 0, 0));
        DiabetesDiary d = saveDiaryService.saveDiary(a, 20, "test1", LocalDateTime.of(2021, 12, 25, 0, 0, 0));

        Diet e = saveDiaryService.saveDiet(a, b, EatTime.BreakFast, 100);
        Diet f = saveDiaryService.saveDiet(a, b, EatTime.Lunch, 100);
        Diet g = saveDiaryService.saveDiet(a, b, EatTime.Dinner, 100);

        Diet h = saveDiaryService.saveDiet(a, c, EatTime.BreakFast, 120);
        Diet i = saveDiaryService.saveDiet(a, c, EatTime.Lunch, 200);
        Diet j = saveDiaryService.saveDiet(a, c, EatTime.Dinner, 170);

        Diet k = saveDiaryService.saveDiet(a, d, EatTime.BreakFast, 150);
        Diet l = saveDiaryService.saveDiet(a, d, EatTime.Lunch, 120);
        Diet m = saveDiaryService.saveDiet(a, d, EatTime.Dinner, 140);

    }

실험 결과

2022-05-15 12:14:49.663  INFO 2860 --- [           main] o.s.t.c.transaction.TransactionContext   : Began transaction (1) for test context [DefaultTestContext@40a4337a testClass = ReadDiaryTest, testInstance = com.dasd412.remake.api.domain.diary.ReadDiaryTest@6edd4fe2, testMethod = test@ReadDiaryTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@6025e1b6 testClass = ReadDiaryTest, locations = '{}', classes = '{class com.dasd412.remake.api.Application}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{classpath:application-test.properties}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@6b57696f, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@38bc8ab5, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@50d0686, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@66d1af89], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.populatedRequestContextHolder' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.resetRequestContextHolder' -> true]]; transaction manager [org.springframework.orm.jpa.JpaTransactionManager@628bd77e]; rollback [true]
2022-05-15 12:14:49.724  INFO 2860 --- [           main] c.d.r.a.service.domain.SaveDiaryService  : saveWriter
2022-05-15 12:14:49.833  INFO 2860 --- [           main] o.h.h.i.QueryTranslatorFactoryInitiator  : HHH000397: Using ASTQueryTranslatorFactory
Hibernate: select max(writer0_.writer_id) as col_0_0_ from writer writer0_
Hibernate: select writer0_.writer_id as writer_i1_4_1_, writer0_.created_at as created_2_4_1_, writer0_.updated_at as updated_3_4_1_, writer0_.email as email4_4_1_, writer0_.name as name5_4_1_, writer0_.password as password6_4_1_, writer0_.profile_id as profile10_4_1_, writer0_.provider as provider7_4_1_, writer0_.provider_id as provider8_4_1_, writer0_.role as role9_4_1_, diaries1_.writer_id as writer_i2_0_3_, diaries1_.diary_id as diary_id1_0_3_, diaries1_.diary_id as diary_id1_0_0_, diaries1_.writer_id as writer_i2_0_0_, diaries1_.created_at as created_3_0_0_, diaries1_.updated_at as updated_4_0_0_, diaries1_.fpg as fpg5_0_0_, diaries1_.remark as remark6_0_0_, diaries1_.written_time as written_7_0_0_ from writer writer0_ left outer join diabetes_diary diaries1_ on writer0_.writer_id=diaries1_.writer_id where writer0_.writer_id=?
2022-05-15 12:14:49.997  INFO 2860 --- [           main] c.d.r.a.service.domain.SaveDiaryService  : saveDiary
Hibernate: insert into writer (created_at, updated_at, email, name, password, profile_id, provider, provider_id, role, writer_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: select max(diabetesdi0_.diary_id) as col_0_0_ from diabetes_diary diabetesdi0_
Hibernate: select diabetesdi0_.diary_id as diary_id1_0_1_, diabetesdi0_.writer_id as writer_i2_0_1_, diabetesdi0_.created_at as created_3_0_1_, diabetesdi0_.updated_at as updated_4_0_1_, diabetesdi0_.fpg as fpg5_0_1_, diabetesdi0_.remark as remark6_0_1_, diabetesdi0_.written_time as written_7_0_1_, dietlist1_.diary_id as diary_id6_1_3_, dietlist1_.writer_id as writer_i7_1_3_, dietlist1_.diary_id as diary_id0_1_3_, dietlist1_.writer_id as writer_i0_1_3_, dietlist1_.diet_id as diet_id1_1_3_, dietlist1_.diary_id as diary_id0_1_0_, dietlist1_.writer_id as writer_i0_1_0_, dietlist1_.diet_id as diet_id1_1_0_, dietlist1_.diary_id as diary_id6_1_0_, dietlist1_.writer_id as writer_i7_1_0_, dietlist1_.created_at as created_2_1_0_, dietlist1_.updated_at as updated_3_1_0_, dietlist1_.blood_sugar as blood_su4_1_0_, dietlist1_.eat_time as eat_time5_1_0_ from diabetes_diary diabetesdi0_ left outer join diet dietlist1_ on diabetesdi0_.diary_id=dietlist1_.diary_id and diabetesdi0_.writer_id=dietlist1_.writer_id where diabetesdi0_.diary_id=? and diabetesdi0_.writer_id=?
2022-05-15 12:14:50.028  INFO 2860 --- [           main] c.d.r.a.service.domain.SaveDiaryService  : saveDiary
Hibernate: insert into diabetes_diary (created_at, updated_at, fpg, remark, written_time, diary_id, writer_id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: update writer set created_at=?, updated_at=?, email=?, name=?, password=?, profile_id=?, provider=?, provider_id=?, role=? where writer_id=?
Hibernate: select max(diabetesdi0_.diary_id) as col_0_0_ from diabetes_diary diabetesdi0_
Hibernate: select diabetesdi0_.diary_id as diary_id1_0_1_, diabetesdi0_.writer_id as writer_i2_0_1_, diabetesdi0_.created_at as created_3_0_1_, diabetesdi0_.updated_at as updated_4_0_1_, diabetesdi0_.fpg as fpg5_0_1_, diabetesdi0_.remark as remark6_0_1_, diabetesdi0_.written_time as written_7_0_1_, dietlist1_.diary_id as diary_id6_1_3_, dietlist1_.writer_id as writer_i7_1_3_, dietlist1_.diary_id as diary_id0_1_3_, dietlist1_.writer_id as writer_i0_1_3_, dietlist1_.diet_id as diet_id1_1_3_, dietlist1_.diary_id as diary_id0_1_0_, dietlist1_.writer_id as writer_i0_1_0_, dietlist1_.diet_id as diet_id1_1_0_, dietlist1_.diary_id as diary_id6_1_0_, dietlist1_.writer_id as writer_i7_1_0_, dietlist1_.created_at as created_2_1_0_, dietlist1_.updated_at as updated_3_1_0_, dietlist1_.blood_sugar as blood_su4_1_0_, dietlist1_.eat_time as eat_time5_1_0_ from diabetes_diary diabetesdi0_ left outer join diet dietlist1_ on diabetesdi0_.diary_id=dietlist1_.diary_id and diabetesdi0_.writer_id=dietlist1_.writer_id where diabetesdi0_.diary_id=? and diabetesdi0_.writer_id=?
2022-05-15 12:14:50.038  INFO 2860 --- [           main] c.d.r.a.service.domain.SaveDiaryService  : saveDiary
Hibernate: insert into diabetes_diary (created_at, updated_at, fpg, remark, written_time, diary_id, writer_id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: update writer set created_at=?, updated_at=?, email=?, name=?, password=?, profile_id=?, provider=?, provider_id=?, role=? where writer_id=?
Hibernate: update diabetes_diary set created_at=?, updated_at=?, fpg=?, remark=?, written_time=? where diary_id=? and writer_id=?
Hibernate: select max(diabetesdi0_.diary_id) as col_0_0_ from diabetes_diary diabetesdi0_
Hibernate: select diabetesdi0_.diary_id as diary_id1_0_1_, diabetesdi0_.writer_id as writer_i2_0_1_, diabetesdi0_.created_at as created_3_0_1_, diabetesdi0_.updated_at as updated_4_0_1_, diabetesdi0_.fpg as fpg5_0_1_, diabetesdi0_.remark as remark6_0_1_, diabetesdi0_.written_time as written_7_0_1_, dietlist1_.diary_id as diary_id6_1_3_, dietlist1_.writer_id as writer_i7_1_3_, dietlist1_.diary_id as diary_id0_1_3_, dietlist1_.writer_id as writer_i0_1_3_, dietlist1_.diet_id as diet_id1_1_3_, dietlist1_.diary_id as diary_id0_1_0_, dietlist1_.writer_id as writer_i0_1_0_, dietlist1_.diet_id as diet_id1_1_0_, dietlist1_.diary_id as diary_id6_1_0_, dietlist1_.writer_id as writer_i7_1_0_, dietlist1_.created_at as created_2_1_0_, dietlist1_.updated_at as updated_3_1_0_, dietlist1_.blood_sugar as blood_su4_1_0_, dietlist1_.eat_time as eat_time5_1_0_ from diabetes_diary diabetesdi0_ left outer join diet dietlist1_ on diabetesdi0_.diary_id=dietlist1_.diary_id and diabetesdi0_.writer_id=dietlist1_.writer_id where diabetesdi0_.diary_id=? and diabetesdi0_.writer_id=?
2022-05-15 12:14:50.048  INFO 2860 --- [           main] c.d.r.a.service.domain.SaveDiaryService  : saveDiet
Hibernate: insert into diabetes_diary (created_at, updated_at, fpg, remark, written_time, diary_id, writer_id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: update writer set created_at=?, updated_at=?, email=?, name=?, password=?, profile_id=?, provider=?, provider_id=?, role=? where writer_id=?
Hibernate: update diabetes_diary set created_at=?, updated_at=?, fpg=?, remark=?, written_time=? where diary_id=? and writer_id=?
Hibernate: update diabetes_diary set created_at=?, updated_at=?, fpg=?, remark=?, written_time=? where diary_id=? and writer_id=?
Hibernate: select max(diet0_.diet_id) as col_0_0_ from diet diet0_
Hibernate: select diet0_.diary_id as diary_id0_1_1_, diet0_.writer_id as writer_i0_1_1_, diet0_.diet_id as diet_id1_1_1_, diet0_.diary_id as diary_id6_1_1_, diet0_.writer_id as writer_i7_1_1_, diet0_.created_at as created_2_1_1_, diet0_.updated_at as updated_3_1_1_, diet0_.blood_sugar as blood_su4_1_1_, diet0_.eat_time as eat_time5_1_1_, foodlist1_.diary_id as diary_id7_2_3_, foodlist1_.writer_id as writer_i8_2_3_, foodlist1_.diet_id as diet_id9_2_3_, foodlist1_.diary_id as diary_id0_2_3_, foodlist1_.writer_id as writer_i0_2_3_, foodlist1_.diet_id as diet_id0_2_3_, foodlist1_.food_id as food_id1_2_3_, foodlist1_.diary_id as diary_id0_2_0_, foodlist1_.writer_id as writer_i0_2_0_, foodlist1_.diet_id as diet_id0_2_0_, foodlist1_.food_id as food_id1_2_0_, foodlist1_.diary_id as diary_id7_2_0_, foodlist1_.writer_id as writer_i8_2_0_, foodlist1_.diet_id as diet_id9_2_0_, foodlist1_.created_at as created_2_2_0_, foodlist1_.updated_at as updated_3_2_0_, foodlist1_.amount as amount4_2_0_, foodlist1_.amount_unit as amount_u5_2_0_, foodlist1_.food_name as food_nam6_2_0_ from diet diet0_ left outer join food foodlist1_ on diet0_.diary_id=foodlist1_.diary_id and diet0_.writer_id=foodlist1_.writer_id and diet0_.diet_id=foodlist1_.diet_id where diet0_.diary_id=? and diet0_.writer_id=? and diet0_.diet_id=?
2022-05-15 12:14:50.058  INFO 2860 --- [           main] c.d.r.a.service.domain.SaveDiaryService  : saveDiet
Hibernate: insert into diet (created_at, updated_at, blood_sugar, eat_time, diary_id, writer_id, diet_id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: update writer set created_at=?, updated_at=?, email=?, name=?, password=?, profile_id=?, provider=?, provider_id=?, role=? where writer_id=?
Hibernate: update diabetes_diary set created_at=?, updated_at=?, fpg=?, remark=?, written_time=? where diary_id=? and writer_id=?
Hibernate: update diabetes_diary set created_at=?, updated_at=?, fpg=?, remark=?, written_time=? where diary_id=? and writer_id=?
Hibernate: update diabetes_diary set created_at=?, updated_at=?, fpg=?, remark=?, written_time=? where diary_id=? and writer_id=?
Hibernate: select max(diet0_.diet_id) as col_0_0_ from diet diet0_
Hibernate: select diet0_.diary_id as diary_id0_1_1_, diet0_.writer_id as writer_i0_1_1_, diet0_.diet_id as diet_id1_1_1_, diet0_.diary_id as diary_id6_1_1_, diet0_.writer_id as writer_i7_1_1_, diet0_.created_at as created_2_1_1_, diet0_.updated_at as updated_3_1_1_, diet0_.blood_sugar as blood_su4_1_1_, diet0_.eat_time as eat_time5_1_1_, foodlist1_.diary_id as diary_id7_2_3_, foodlist1_.writer_id as writer_i8_2_3_, foodlist1_.diet_id as diet_id9_2_3_, foodlist1_.diary_id as diary_id0_2_3_, foodlist1_.writer_id as writer_i0_2_3_, foodlist1_.diet_id as diet_id0_2_3_, foodlist1_.food_id as food_id1_2_3_, foodlist1_.diary_id as diary_id0_2_0_, foodlist1_.writer_id as writer_i0_2_0_, foodlist1_.diet_id as diet_id0_2_0_, foodlist1_.food_id as food_id1_2_0_, foodlist1_.diary_id as diary_id7_2_0_, foodlist1_.writer_id as writer_i8_2_0_, foodlist1_.diet_id as diet_id9_2_0_, foodlist1_.created_at as created_2_2_0_, foodlist1_.updated_at as updated_3_2_0_, foodlist1_.amount as amount4_2_0_, foodlist1_.amount_unit as amount_u5_2_0_, foodlist1_.food_name as food_nam6_2_0_ from diet diet0_ left outer join food foodlist1_ on diet0_.diary_id=foodlist1_.diary_id and diet0_.writer_id=foodlist1_.writer_id and diet0_.diet_id=foodlist1_.diet_id where diet0_.diary_id=? and diet0_.writer_id=? and diet0_.diet_id=?
2022-05-15 12:14:50.068  INFO 2860 --- [           main] c.d.r.a.service.domain.SaveDiaryService  : saveDiet
Hibernate: insert into diet (created_at, updated_at, blood_sugar, eat_time, diary_id, writer_id, diet_id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: update writer set created_at=?, updated_at=?, email=?, name=?, password=?, profile_id=?, provider=?, provider_id=?, role=? where writer_id=?
Hibernate: update diabetes_diary set created_at=?, updated_at=?, fpg=?, remark=?, written_time=? where diary_id=? and writer_id=?
Hibernate: update diabetes_diary set created_at=?, updated_at=?, fpg=?, remark=?, written_time=? where diary_id=? and writer_id=?
Hibernate: update diabetes_diary set created_at=?, updated_at=?, fpg=?, remark=?, written_time=? where diary_id=? and writer_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: select max(diet0_.diet_id) as col_0_0_ from diet diet0_
Hibernate: select diet0_.diary_id as diary_id0_1_1_, diet0_.writer_id as writer_i0_1_1_, diet0_.diet_id as diet_id1_1_1_, diet0_.diary_id as diary_id6_1_1_, diet0_.writer_id as writer_i7_1_1_, diet0_.created_at as created_2_1_1_, diet0_.updated_at as updated_3_1_1_, diet0_.blood_sugar as blood_su4_1_1_, diet0_.eat_time as eat_time5_1_1_, foodlist1_.diary_id as diary_id7_2_3_, foodlist1_.writer_id as writer_i8_2_3_, foodlist1_.diet_id as diet_id9_2_3_, foodlist1_.diary_id as diary_id0_2_3_, foodlist1_.writer_id as writer_i0_2_3_, foodlist1_.diet_id as diet_id0_2_3_, foodlist1_.food_id as food_id1_2_3_, foodlist1_.diary_id as diary_id0_2_0_, foodlist1_.writer_id as writer_i0_2_0_, foodlist1_.diet_id as diet_id0_2_0_, foodlist1_.food_id as food_id1_2_0_, foodlist1_.diary_id as diary_id7_2_0_, foodlist1_.writer_id as writer_i8_2_0_, foodlist1_.diet_id as diet_id9_2_0_, foodlist1_.created_at as created_2_2_0_, foodlist1_.updated_at as updated_3_2_0_, foodlist1_.amount as amount4_2_0_, foodlist1_.amount_unit as amount_u5_2_0_, foodlist1_.food_name as food_nam6_2_0_ from diet diet0_ left outer join food foodlist1_ on diet0_.diary_id=foodlist1_.diary_id and diet0_.writer_id=foodlist1_.writer_id and diet0_.diet_id=foodlist1_.diet_id where diet0_.diary_id=? and diet0_.writer_id=? and diet0_.diet_id=?
2022-05-15 12:14:50.078  INFO 2860 --- [           main] c.d.r.a.service.domain.SaveDiaryService  : saveDiet
Hibernate: insert into diet (created_at, updated_at, blood_sugar, eat_time, diary_id, writer_id, diet_id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: update writer set created_at=?, updated_at=?, email=?, name=?, password=?, profile_id=?, provider=?, provider_id=?, role=? where writer_id=?
Hibernate: update diabetes_diary set created_at=?, updated_at=?, fpg=?, remark=?, written_time=? where diary_id=? and writer_id=?
Hibernate: update diabetes_diary set created_at=?, updated_at=?, fpg=?, remark=?, written_time=? where diary_id=? and writer_id=?
Hibernate: update diabetes_diary set created_at=?, updated_at=?, fpg=?, remark=?, written_time=? where diary_id=? and writer_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: select max(diet0_.diet_id) as col_0_0_ from diet diet0_
Hibernate: select diet0_.diary_id as diary_id0_1_1_, diet0_.writer_id as writer_i0_1_1_, diet0_.diet_id as diet_id1_1_1_, diet0_.diary_id as diary_id6_1_1_, diet0_.writer_id as writer_i7_1_1_, diet0_.created_at as created_2_1_1_, diet0_.updated_at as updated_3_1_1_, diet0_.blood_sugar as blood_su4_1_1_, diet0_.eat_time as eat_time5_1_1_, foodlist1_.diary_id as diary_id7_2_3_, foodlist1_.writer_id as writer_i8_2_3_, foodlist1_.diet_id as diet_id9_2_3_, foodlist1_.diary_id as diary_id0_2_3_, foodlist1_.writer_id as writer_i0_2_3_, foodlist1_.diet_id as diet_id0_2_3_, foodlist1_.food_id as food_id1_2_3_, foodlist1_.diary_id as diary_id0_2_0_, foodlist1_.writer_id as writer_i0_2_0_, foodlist1_.diet_id as diet_id0_2_0_, foodlist1_.food_id as food_id1_2_0_, foodlist1_.diary_id as diary_id7_2_0_, foodlist1_.writer_id as writer_i8_2_0_, foodlist1_.diet_id as diet_id9_2_0_, foodlist1_.created_at as created_2_2_0_, foodlist1_.updated_at as updated_3_2_0_, foodlist1_.amount as amount4_2_0_, foodlist1_.amount_unit as amount_u5_2_0_, foodlist1_.food_name as food_nam6_2_0_ from diet diet0_ left outer join food foodlist1_ on diet0_.diary_id=foodlist1_.diary_id and diet0_.writer_id=foodlist1_.writer_id and diet0_.diet_id=foodlist1_.diet_id where diet0_.diary_id=? and diet0_.writer_id=? and diet0_.diet_id=?
2022-05-15 12:14:50.088  INFO 2860 --- [           main] c.d.r.a.service.domain.SaveDiaryService  : saveDiet
Hibernate: insert into diet (created_at, updated_at, blood_sugar, eat_time, diary_id, writer_id, diet_id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: update writer set created_at=?, updated_at=?, email=?, name=?, password=?, profile_id=?, provider=?, provider_id=?, role=? where writer_id=?
Hibernate: update diabetes_diary set created_at=?, updated_at=?, fpg=?, remark=?, written_time=? where diary_id=? and writer_id=?
Hibernate: update diabetes_diary set created_at=?, updated_at=?, fpg=?, remark=?, written_time=? where diary_id=? and writer_id=?
Hibernate: update diabetes_diary set created_at=?, updated_at=?, fpg=?, remark=?, written_time=? where diary_id=? and writer_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: select max(diet0_.diet_id) as col_0_0_ from diet diet0_
Hibernate: select diet0_.diary_id as diary_id0_1_1_, diet0_.writer_id as writer_i0_1_1_, diet0_.diet_id as diet_id1_1_1_, diet0_.diary_id as diary_id6_1_1_, diet0_.writer_id as writer_i7_1_1_, diet0_.created_at as created_2_1_1_, diet0_.updated_at as updated_3_1_1_, diet0_.blood_sugar as blood_su4_1_1_, diet0_.eat_time as eat_time5_1_1_, foodlist1_.diary_id as diary_id7_2_3_, foodlist1_.writer_id as writer_i8_2_3_, foodlist1_.diet_id as diet_id9_2_3_, foodlist1_.diary_id as diary_id0_2_3_, foodlist1_.writer_id as writer_i0_2_3_, foodlist1_.diet_id as diet_id0_2_3_, foodlist1_.food_id as food_id1_2_3_, foodlist1_.diary_id as diary_id0_2_0_, foodlist1_.writer_id as writer_i0_2_0_, foodlist1_.diet_id as diet_id0_2_0_, foodlist1_.food_id as food_id1_2_0_, foodlist1_.diary_id as diary_id7_2_0_, foodlist1_.writer_id as writer_i8_2_0_, foodlist1_.diet_id as diet_id9_2_0_, foodlist1_.created_at as created_2_2_0_, foodlist1_.updated_at as updated_3_2_0_, foodlist1_.amount as amount4_2_0_, foodlist1_.amount_unit as amount_u5_2_0_, foodlist1_.food_name as food_nam6_2_0_ from diet diet0_ left outer join food foodlist1_ on diet0_.diary_id=foodlist1_.diary_id and diet0_.writer_id=foodlist1_.writer_id and diet0_.diet_id=foodlist1_.diet_id where diet0_.diary_id=? and diet0_.writer_id=? and diet0_.diet_id=?
2022-05-15 12:14:50.096  INFO 2860 --- [           main] c.d.r.a.service.domain.SaveDiaryService  : saveDiet
Hibernate: insert into diet (created_at, updated_at, blood_sugar, eat_time, diary_id, writer_id, diet_id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: update writer set created_at=?, updated_at=?, email=?, name=?, password=?, profile_id=?, provider=?, provider_id=?, role=? where writer_id=?
Hibernate: update diabetes_diary set created_at=?, updated_at=?, fpg=?, remark=?, written_time=? where diary_id=? and writer_id=?
Hibernate: update diabetes_diary set created_at=?, updated_at=?, fpg=?, remark=?, written_time=? where diary_id=? and writer_id=?
Hibernate: update diabetes_diary set created_at=?, updated_at=?, fpg=?, remark=?, written_time=? where diary_id=? and writer_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: select max(diet0_.diet_id) as col_0_0_ from diet diet0_
Hibernate: select diet0_.diary_id as diary_id0_1_1_, diet0_.writer_id as writer_i0_1_1_, diet0_.diet_id as diet_id1_1_1_, diet0_.diary_id as diary_id6_1_1_, diet0_.writer_id as writer_i7_1_1_, diet0_.created_at as created_2_1_1_, diet0_.updated_at as updated_3_1_1_, diet0_.blood_sugar as blood_su4_1_1_, diet0_.eat_time as eat_time5_1_1_, foodlist1_.diary_id as diary_id7_2_3_, foodlist1_.writer_id as writer_i8_2_3_, foodlist1_.diet_id as diet_id9_2_3_, foodlist1_.diary_id as diary_id0_2_3_, foodlist1_.writer_id as writer_i0_2_3_, foodlist1_.diet_id as diet_id0_2_3_, foodlist1_.food_id as food_id1_2_3_, foodlist1_.diary_id as diary_id0_2_0_, foodlist1_.writer_id as writer_i0_2_0_, foodlist1_.diet_id as diet_id0_2_0_, foodlist1_.food_id as food_id1_2_0_, foodlist1_.diary_id as diary_id7_2_0_, foodlist1_.writer_id as writer_i8_2_0_, foodlist1_.diet_id as diet_id9_2_0_, foodlist1_.created_at as created_2_2_0_, foodlist1_.updated_at as updated_3_2_0_, foodlist1_.amount as amount4_2_0_, foodlist1_.amount_unit as amount_u5_2_0_, foodlist1_.food_name as food_nam6_2_0_ from diet diet0_ left outer join food foodlist1_ on diet0_.diary_id=foodlist1_.diary_id and diet0_.writer_id=foodlist1_.writer_id and diet0_.diet_id=foodlist1_.diet_id where diet0_.diary_id=? and diet0_.writer_id=? and diet0_.diet_id=?
2022-05-15 12:14:50.106  INFO 2860 --- [           main] c.d.r.a.service.domain.SaveDiaryService  : saveDiet
Hibernate: insert into diet (created_at, updated_at, blood_sugar, eat_time, diary_id, writer_id, diet_id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: update writer set created_at=?, updated_at=?, email=?, name=?, password=?, profile_id=?, provider=?, provider_id=?, role=? where writer_id=?
Hibernate: update diabetes_diary set created_at=?, updated_at=?, fpg=?, remark=?, written_time=? where diary_id=? and writer_id=?
Hibernate: update diabetes_diary set created_at=?, updated_at=?, fpg=?, remark=?, written_time=? where diary_id=? and writer_id=?
Hibernate: update diabetes_diary set created_at=?, updated_at=?, fpg=?, remark=?, written_time=? where diary_id=? and writer_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: select max(diet0_.diet_id) as col_0_0_ from diet diet0_
Hibernate: select diet0_.diary_id as diary_id0_1_1_, diet0_.writer_id as writer_i0_1_1_, diet0_.diet_id as diet_id1_1_1_, diet0_.diary_id as diary_id6_1_1_, diet0_.writer_id as writer_i7_1_1_, diet0_.created_at as created_2_1_1_, diet0_.updated_at as updated_3_1_1_, diet0_.blood_sugar as blood_su4_1_1_, diet0_.eat_time as eat_time5_1_1_, foodlist1_.diary_id as diary_id7_2_3_, foodlist1_.writer_id as writer_i8_2_3_, foodlist1_.diet_id as diet_id9_2_3_, foodlist1_.diary_id as diary_id0_2_3_, foodlist1_.writer_id as writer_i0_2_3_, foodlist1_.diet_id as diet_id0_2_3_, foodlist1_.food_id as food_id1_2_3_, foodlist1_.diary_id as diary_id0_2_0_, foodlist1_.writer_id as writer_i0_2_0_, foodlist1_.diet_id as diet_id0_2_0_, foodlist1_.food_id as food_id1_2_0_, foodlist1_.diary_id as diary_id7_2_0_, foodlist1_.writer_id as writer_i8_2_0_, foodlist1_.diet_id as diet_id9_2_0_, foodlist1_.created_at as created_2_2_0_, foodlist1_.updated_at as updated_3_2_0_, foodlist1_.amount as amount4_2_0_, foodlist1_.amount_unit as amount_u5_2_0_, foodlist1_.food_name as food_nam6_2_0_ from diet diet0_ left outer join food foodlist1_ on diet0_.diary_id=foodlist1_.diary_id and diet0_.writer_id=foodlist1_.writer_id and diet0_.diet_id=foodlist1_.diet_id where diet0_.diary_id=? and diet0_.writer_id=? and diet0_.diet_id=?
2022-05-15 12:14:50.116  INFO 2860 --- [           main] c.d.r.a.service.domain.SaveDiaryService  : saveDiet
Hibernate: insert into diet (created_at, updated_at, blood_sugar, eat_time, diary_id, writer_id, diet_id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: update writer set created_at=?, updated_at=?, email=?, name=?, password=?, profile_id=?, provider=?, provider_id=?, role=? where writer_id=?
Hibernate: update diabetes_diary set created_at=?, updated_at=?, fpg=?, remark=?, written_time=? where diary_id=? and writer_id=?
Hibernate: update diabetes_diary set created_at=?, updated_at=?, fpg=?, remark=?, written_time=? where diary_id=? and writer_id=?
Hibernate: update diabetes_diary set created_at=?, updated_at=?, fpg=?, remark=?, written_time=? where diary_id=? and writer_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: select max(diet0_.diet_id) as col_0_0_ from diet diet0_
Hibernate: select diet0_.diary_id as diary_id0_1_1_, diet0_.writer_id as writer_i0_1_1_, diet0_.diet_id as diet_id1_1_1_, diet0_.diary_id as diary_id6_1_1_, diet0_.writer_id as writer_i7_1_1_, diet0_.created_at as created_2_1_1_, diet0_.updated_at as updated_3_1_1_, diet0_.blood_sugar as blood_su4_1_1_, diet0_.eat_time as eat_time5_1_1_, foodlist1_.diary_id as diary_id7_2_3_, foodlist1_.writer_id as writer_i8_2_3_, foodlist1_.diet_id as diet_id9_2_3_, foodlist1_.diary_id as diary_id0_2_3_, foodlist1_.writer_id as writer_i0_2_3_, foodlist1_.diet_id as diet_id0_2_3_, foodlist1_.food_id as food_id1_2_3_, foodlist1_.diary_id as diary_id0_2_0_, foodlist1_.writer_id as writer_i0_2_0_, foodlist1_.diet_id as diet_id0_2_0_, foodlist1_.food_id as food_id1_2_0_, foodlist1_.diary_id as diary_id7_2_0_, foodlist1_.writer_id as writer_i8_2_0_, foodlist1_.diet_id as diet_id9_2_0_, foodlist1_.created_at as created_2_2_0_, foodlist1_.updated_at as updated_3_2_0_, foodlist1_.amount as amount4_2_0_, foodlist1_.amount_unit as amount_u5_2_0_, foodlist1_.food_name as food_nam6_2_0_ from diet diet0_ left outer join food foodlist1_ on diet0_.diary_id=foodlist1_.diary_id and diet0_.writer_id=foodlist1_.writer_id and diet0_.diet_id=foodlist1_.diet_id where diet0_.diary_id=? and diet0_.writer_id=? and diet0_.diet_id=?
2022-05-15 12:14:50.127  INFO 2860 --- [           main] c.d.r.a.service.domain.SaveDiaryService  : saveDiet
Hibernate: insert into diet (created_at, updated_at, blood_sugar, eat_time, diary_id, writer_id, diet_id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: update writer set created_at=?, updated_at=?, email=?, name=?, password=?, profile_id=?, provider=?, provider_id=?, role=? where writer_id=?
Hibernate: update diabetes_diary set created_at=?, updated_at=?, fpg=?, remark=?, written_time=? where diary_id=? and writer_id=?
Hibernate: update diabetes_diary set created_at=?, updated_at=?, fpg=?, remark=?, written_time=? where diary_id=? and writer_id=?
Hibernate: update diabetes_diary set created_at=?, updated_at=?, fpg=?, remark=?, written_time=? where diary_id=? and writer_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: update diet set created_at=?, updated_at=?, blood_sugar=?, eat_time=? where diary_id=? and writer_id=? and diet_id=?
Hibernate: select max(diet0_.diet_id) as col_0_0_ from diet diet0_
Hibernate: select diet0_.diary_id as diary_id0_1_1_, diet0_.writer_id as writer_i0_1_1_, diet0_.diet_id as diet_id1_1_1_, diet0_.diary_id as diary_id6_1_1_, diet0_.writer_id as writer_i7_1_1_, diet0_.created_at as created_2_1_1_, diet0_.updated_at as updated_3_1_1_, diet0_.blood_sugar as blood_su4_1_1_, diet0_.eat_time as eat_time5_1_1_, foodlist1_.diary_id as diary_id7_2_3_, foodlist1_.writer_id as writer_i8_2_3_, foodlist1_.diet_id as diet_id9_2_3_, foodlist1_.diary_id as diary_id0_2_3_, foodlist1_.writer_id as writer_i0_2_3_, foodlist1_.diet_id as diet_id0_2_3_, foodlist1_.food_id as food_id1_2_3_, foodlist1_.diary_id as diary_id0_2_0_, foodlist1_.writer_id as writer_i0_2_0_, foodlist1_.diet_id as diet_id0_2_0_, foodlist1_.food_id as food_id1_2_0_, foodlist1_.diary_id as diary_id7_2_0_, foodlist1_.writer_id as writer_i8_2_0_, foodlist1_.diet_id as diet_id9_2_0_, foodlist1_.created_at as created_2_2_0_, foodlist1_.updated_at as updated_3_2_0_, foodlist1_.amount as amount4_2_0_, foodlist1_.amount_unit as amount_u5_2_0_, foodlist1_.food_name as food_nam6_2_0_ from diet diet0_ left outer join food foodlist1_ on diet0_.diary_id=foodlist1_.diary_id and diet0_.writer_id=foodlist1_.writer_id and diet0_.diet_id=foodlist1_.diet_id where diet0_.diary_id=? and diet0_.writer_id=? and diet0_.diet_id=?

동일하게 메서드를 만들어 실험했더니 동일한 update 쿼리 문제가 발생했다.


코드 비교

문제가 발생하는 코드는 다음과 같다.

 @Transactional
    public DiabetesDiary saveDiary(Writer writer, int fastingPlasmaGlucose, String remark, LocalDateTime writtenTime) {
        logger.info("saveDiary");
        DiabetesDiary diary = new DiabetesDiary(getNextIdOfDiary(), writer, fastingPlasmaGlucose, remark, writtenTime);
        writer.addDiary(diary);
        writerRepository.save(writer);
        return diary;
    }

문제가 발생하지 않는 코드는 다음과 같다.

    @Transactional
    public DiabetesDiary saveDiaryOfWriterById(EntityId<Writer, Long> writerEntityId, int fastingPlasmaGlucose, String remark, LocalDateTime writtenTime) {
        logger.info("saveDiaryOfWriterById");
        checkNotNull(writerEntityId, "writerId must be provided");

        Writer writer = writerRepository.findById(writerEntityId.getId()).orElseThrow(() -> new NoResultException("작성자가 없습니다."));
        DiabetesDiary diary = new DiabetesDiary(getNextIdOfDiary(), writer, fastingPlasmaGlucose, remark, writtenTime);
        writer.addDiary(diary);
        writerRepository.save(writer);
        return diary;
    }

비교해보면, 문제가 발생하는 코드에서는 엔티티를 외부 클라이언트에서 파라미터로 받아온다. 반면 문제가 발생하지 않는 코드는 엔티티의 id를 파라미터로 받고 메서드 내에서 해당 id의 엔티티를 find()한다.

로그를 까봤는데 DefaultFlushEntityEventListenerAbstractEntityPersister에서 문제가 일어나는 것 같다. 흠..

2022-05-15 12:52:43.677 TRACE 11412 --- [           main] o.h.e.i.DefaultFlushEntityEventListener  : Found dirty properties [[com.dasd412.remake.api.domain.diary.diet.Diet#com.dasd412.remake.api.domain.diary.diet.DietId@7c01]] : [updatedAt]
2022-05-15 12:52:43.677 TRACE 11412 --- [           main] o.h.e.i.DefaultFlushEntityEventListener  : Updating entity: [com.dasd412.remake.api.domain.diary.diet.Diet#component[diary,dietId]{diary=component[diaryId,writer]{diaryId=1, writer=1}, dietId=1}]
2022-05-15 12:52:43.677 TRACE 11412 --- [           main] o.h.p.entity.AbstractEntityPersister     : com.dasd412.remake.api.domain.diary.diet.Diet.updatedAt is dirty

결론

현재는 문제가 발생하지 않는 코드 버전이기 때문에 모든 하위 엔티티에 생성 시간, 갱신 시간 속성을 넣어도 무방하다. 하지만, 위에서도 언급했듯이 식단과 음식은 어차피 일지에 완전 종속되기 때문에 식단과 음식에는 해당 속성을 넣을 필요가 없어서 넣지 않는다.

그리고 불필요한 update 쿼리가 발생하지 않게 하려면, 파라미터를 엔티티로 바로 받지 말고 id를 파라미터로 받고 메서드 내에서 find()해야 하는 것으로 보인다.

찾아보니 JPA dirty check는 변화의 기준이 최초 조회 상태라고 한다.
문제가 생기는 코드는 최초 조회가 트랜잭션 바깥의 메서드 외부에서 일어난다.
문제가 생기지 않는 코드는 최초 조회가 트랜잭션에 둘러 싸인 메서드 내부에서 일어난다.

따라서 불필요한 dirty check가 일어나지 않기 위해서는
엔티티 최초 조회를 같은 트랜잭션 내의 메서드 내부에서 해야 하는 것 같다.


요약

  1. 문제 상황
    1:N 관계로 이루어진 엔티티들이 사슬처럼 맞물린 상황에서 엔티티 save시 update 쿼리가 추가 발생한다.
  1. 원인
    2-1. 엔티티들이 갱신 시간을 자동 추적하는 추상 클래스 BaseTimeEntity를 상속받는다.
    2-2. 하위 엔티티 저장 메소드가 save(상위 엔티티 id)가 아닌 save(엔티티 그 자체)로 되어 있다.
    2-3. 클라이언트 코드에서 먼저 상위 엔티티를 조회해놓는다. 이 때 상위 엔티티에 대한 스냅샷이 찍힌다.
    2-4. 클라이언트가 하위 엔티티 메서드인 save(상위 엔티티)를 호출한다.
    2-5. save(상위 엔티티) 메서드 내에서는 상위 엔티티를 따로 조회하지 않고, 받은 파라미터(상위 엔티티)로 새로 만들어진 하위 엔티티와 연관 관계를 맺는다.
    2-6. save(상위 엔티티) 형태 코드들은 writerRepository.save(writer);로 끝난다. 아마도 이 때 최초 조회 상태의 스냅샷과 현재 영속성 컨텍스트 내 엔티티를 비교하는 것 같다.
    여기서 클라이언트에서 조회했던 엔티티 스냅샷(==Writer)과 다르다고 판정되어 dirty check가 일어나는 것 같다.
    2-7. cascade에 의해 Writer 하위 컬렉션에도 dirty check가 전파된다.
  1. 해결
    엔티티 최초 조회를 같은 트랜잭션 내 메서드 내부에서 한다.

profile
시드 레벨 스타트업의 2호 직원으로서 백엔드 시스템의 70%를 설계 및 개발하였고, TIPS 5억 투자 유치에 기여한 서버 개발자입니다. (Go/Python/MSA/Spring)

0개의 댓글