Skip to content

Commit

Permalink
refactor : member - state/comment/star - like CASCADE config
Browse files Browse the repository at this point in the history
  • Loading branch information
jwpark1211 committed May 16, 2024
1 parent f0724fe commit 719b20c
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/main/java/capstone/bookitty/domain/entity/BookState.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/capstone/bookitty/domain/entity/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/capstone/bookitty/domain/entity/Like.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/capstone/bookitty/domain/entity/Star.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Like> relatedLike = likeRepository.findByCommentId(commentId);
for(Like like : relatedLike){ likeRepository.delete(like); }
commentRepository.delete(comment);
}

Expand Down

0 comments on commit 719b20c

Please sign in to comment.