delete 쿼리가 발생하지 않을때

sonnng·2024년 2월 11일
0

Spring

목록 보기
40/41
post-thumbnail

프론트엔드분께서 대댓글 delete 요청이 안되는 것 같다고 의견을 주셨다. postman으로 확인해보니 삭제요청은 잘 되었다고 메세지가 뜨는데, 쿼리를 살펴보니 delete 쿼리는 나타나지 않았던 것이다.

------------------
2024-02-11 23:04:32.670 DEBUG 19676 --- [nio-8080-exec-2] org.hibernate.SQL                        : 
    select
        child0_.parent_id as parent_i7_9_1_,
        child0_.reply_id as reply_id1_9_1_,
        child0_.reply_id as reply_id1_9_0_,
        child0_.created_at as created_2_9_0_,
        child0_.updated_at as updated_3_9_0_,
        child0_.community_id as communit6_9_0_,
        child0_.content as content4_9_0_,
        child0_.like_count as like_cou5_9_0_,
        child0_.parent_id as parent_i7_9_0_,
        child0_.user_id as user_id8_9_0_ 
    from
        reply child0_ 
    where
        child0_.parent_id in (
            ?, ?
        )
Hibernate: 
    select
        child0_.parent_id as parent_i7_9_1_,
        child0_.reply_id as reply_id1_9_1_,
        child0_.reply_id as reply_id1_9_0_,
        child0_.created_at as created_2_9_0_,
        child0_.updated_at as updated_3_9_0_,
        child0_.community_id as communit6_9_0_,
        child0_.content as content4_9_0_,
        child0_.like_count as like_cou5_9_0_,
        child0_.parent_id as parent_i7_9_0_,
        child0_.user_id as user_id8_9_0_ 
    from
        reply child0_ 
    where
        child0_.parent_id in (
            ?, ?
        )
2024-02-11 23:04:32.670  INFO 19676 --- [nio-8080-exec-2] p6spy                                    : #1707660272670 | took 1ms | statement | connection 5| url jdbc:mysql://localhost:3306/ottifydbtest?serverTimezone=Asia/Seoul
select child0_.parent_id as parent_i7_9_1_, child0_.reply_id as reply_id1_9_1_, child0_.reply_id as reply_id1_9_0_, child0_.created_at as created_2_9_0_, child0_.updated_at as updated_3_9_0_, child0_.community_id as communit6_9_0_, child0_.content as content4_9_0_, child0_.like_count as like_cou5_9_0_, child0_.parent_id as parent_i7_9_0_, child0_.user_id as user_id8_9_0_ from reply child0_ where child0_.parent_id in (?, ?)
select child0_.parent_id as parent_i7_9_1_, child0_.reply_id as reply_id1_9_1_, child0_.reply_id as reply_id1_9_0_, child0_.created_at as created_2_9_0_, child0_.updated_at as updated_3_9_0_, child0_.community_id as communit6_9_0_, child0_.content as content4_9_0_, child0_.like_count as like_cou5_9_0_, child0_.parent_id as parent_i7_9_0_, child0_.user_id as user_id8_9_0_ from reply child0_ where child0_.parent_id in (3, 1);
------------------

대댓글은 삭제하려는 Community, 부모 Reply가 테이블에 존재하는지 확인을 하고 대댓글을 삭제해주도록 했었다. 그러면 community테이블, reply 테이블에 select 쿼리가 발생하면서 해당 테이블에 있는 대댓글 엔티티 정보는 영속상태가 된다. 찾아보니 이렇게 관련된 엔티티를 조회하고나서 delete 요청시 영속상태에 있는 대댓글 엔티티들로 인해 동시성 문제가 나타지 않도록 jpa에서는 삭제쿼리를 날리지 못하는 거라고 한다.


📍delete쿼리가 나가지 않는다면 해결방법

✅ 양방향 연관관계에 있다면, 삭제 연관관계 편의 메소드 작성해주기
✅ 단방향 연관관계에 있다면, select 쿼리를 제외해서 jpa delete하기


++ 댓글 삭제api에서도 community 테이블 select 후 댓글 삭제를 하는데, 여기서는 delete 쿼리가 정상적으로 나가는게 확인된다. 왜 대댓글 삭제API에서만 이런 일이 발생하는지 아직은 잘 모르겠다. jpa 관련 지식이 아직 부족하다고 느껴져서 추가적으로 공부하면서 기록하겠다.

0개의 댓글