From 719b20c555bf311c70deb180e9e5ffda70e7aad9 Mon Sep 17 00:00:00 2001 From: jwpark1211 <1211abc@naver.com> Date: Thu, 16 May 2024 19:54:10 +0900 Subject: [PATCH] refactor : member - state/comment/star - like CASCADE config --- src/main/java/capstone/bookitty/domain/entity/BookState.java | 3 +++ src/main/java/capstone/bookitty/domain/entity/Comment.java | 3 +++ src/main/java/capstone/bookitty/domain/entity/Like.java | 4 ++++ src/main/java/capstone/bookitty/domain/entity/Star.java | 3 +++ .../java/capstone/bookitty/domain/service/CommentService.java | 4 ---- 5 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/main/java/capstone/bookitty/domain/entity/BookState.java b/src/main/java/capstone/bookitty/domain/entity/BookState.java index f35e631..2bfc1d1 100644 --- a/src/main/java/capstone/bookitty/domain/entity/BookState.java +++ b/src/main/java/capstone/bookitty/domain/entity/BookState.java @@ -5,6 +5,8 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; import org.springframework.format.annotation.DateTimeFormat; import java.time.LocalDateTime; @@ -20,6 +22,7 @@ public class BookState { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "member_id") + @OnDelete(action = OnDeleteAction.CASCADE) private Member member; private String isbn; diff --git a/src/main/java/capstone/bookitty/domain/entity/Comment.java b/src/main/java/capstone/bookitty/domain/entity/Comment.java index 3f0ba25..33ab4bc 100644 --- a/src/main/java/capstone/bookitty/domain/entity/Comment.java +++ b/src/main/java/capstone/bookitty/domain/entity/Comment.java @@ -5,6 +5,8 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; import org.springframework.format.annotation.DateTimeFormat; import java.time.LocalDateTime; @@ -20,6 +22,7 @@ public class Comment { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "member_id") + @OnDelete(action = OnDeleteAction.CASCADE) private Member member; private String isbn; diff --git a/src/main/java/capstone/bookitty/domain/entity/Like.java b/src/main/java/capstone/bookitty/domain/entity/Like.java index 14aa0ec..79d13b0 100644 --- a/src/main/java/capstone/bookitty/domain/entity/Like.java +++ b/src/main/java/capstone/bookitty/domain/entity/Like.java @@ -5,6 +5,8 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; import org.springframework.format.annotation.DateTimeFormat; import java.time.LocalDateTime; @@ -20,10 +22,12 @@ public class Like { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "member_id") + @OnDelete(action = OnDeleteAction.CASCADE) private Member member; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "comment_id") + @OnDelete(action = OnDeleteAction.CASCADE) private Comment comment; @DateTimeFormat(pattern = "yyyy-mm-dd'T'HH:mm:ss") diff --git a/src/main/java/capstone/bookitty/domain/entity/Star.java b/src/main/java/capstone/bookitty/domain/entity/Star.java index 500ef19..b71d52b 100644 --- a/src/main/java/capstone/bookitty/domain/entity/Star.java +++ b/src/main/java/capstone/bookitty/domain/entity/Star.java @@ -5,6 +5,8 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; import org.springframework.format.annotation.DateTimeFormat; import java.time.LocalDateTime; @@ -20,6 +22,7 @@ public class Star { @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name = "member_id") + @OnDelete(action = OnDeleteAction.CASCADE) private Member member; private String isbn; diff --git a/src/main/java/capstone/bookitty/domain/service/CommentService.java b/src/main/java/capstone/bookitty/domain/service/CommentService.java index 4e102e5..159c42b 100644 --- a/src/main/java/capstone/bookitty/domain/service/CommentService.java +++ b/src/main/java/capstone/bookitty/domain/service/CommentService.java @@ -13,8 +13,6 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.List; - import static capstone.bookitty.domain.dto.CommentDTO.*; @Service @@ -88,8 +86,6 @@ public void updateComment(Long commentId, UpdateRequest request) { public void deleteComment(Long commentId) { Comment comment = commentRepository.findById(commentId) .orElseThrow(() -> new EntityNotFoundException("Comment with ID " + commentId + " not found.")); - List relatedLike = likeRepository.findByCommentId(commentId); - for(Like like : relatedLike){ likeRepository.delete(like); } commentRepository.delete(comment); }