diff --git a/src/main/java/hamsung/hamsung_project/entity/Comment.java b/src/main/java/hamsung/hamsung_project/entity/Comment.java index 90c67ee..6ae93be 100644 --- a/src/main/java/hamsung/hamsung_project/entity/Comment.java +++ b/src/main/java/hamsung/hamsung_project/entity/Comment.java @@ -2,6 +2,8 @@ import jakarta.persistence.*; import lombok.*; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; import java.util.ArrayList; import java.util.List; @@ -30,8 +32,9 @@ public class Comment { @OneToMany(mappedBy = "comment", cascade = CascadeType.REMOVE, orphanRemoval = true) private List childs = new ArrayList<>(); - @ManyToOne + @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name ="recruit_id") + @OnDelete(action= OnDeleteAction.CASCADE) private Recruit recruit; //스터디 모집글 public static Comment createComment(Long id, String text, String createdDate, String modifiedDate) { diff --git a/src/main/java/hamsung/hamsung_project/entity/Recruit.java b/src/main/java/hamsung/hamsung_project/entity/Recruit.java index 1eff63a..c36c1d7 100644 --- a/src/main/java/hamsung/hamsung_project/entity/Recruit.java +++ b/src/main/java/hamsung/hamsung_project/entity/Recruit.java @@ -35,7 +35,7 @@ public class Recruit { @JoinColumn(name="study_id") - @JsonBackReference +// @JsonBackReference @OneToOne(cascade = CascadeType.ALL) private Study study; diff --git a/src/main/java/hamsung/hamsung_project/entity/StudyMember.java b/src/main/java/hamsung/hamsung_project/entity/StudyMember.java index 2f77968..765bfa5 100644 --- a/src/main/java/hamsung/hamsung_project/entity/StudyMember.java +++ b/src/main/java/hamsung/hamsung_project/entity/StudyMember.java @@ -1,5 +1,6 @@ package hamsung.hamsung_project.entity; +import com.fasterxml.jackson.annotation.JsonBackReference; import jakarta.persistence.*; import lombok.*; import org.hibernate.annotations.ColumnDefault; @@ -30,6 +31,7 @@ public class StudyMember { private User users; @ManyToOne + @JsonBackReference @JoinColumn(name="study_id") private Study study; diff --git a/src/main/java/hamsung/hamsung_project/service/RecruitsService.java b/src/main/java/hamsung/hamsung_project/service/RecruitsService.java index 533c31d..a32746c 100644 --- a/src/main/java/hamsung/hamsung_project/service/RecruitsService.java +++ b/src/main/java/hamsung/hamsung_project/service/RecruitsService.java @@ -2,6 +2,7 @@ import hamsung.hamsung_project.dto.*; import hamsung.hamsung_project.entity.*; +import hamsung.hamsung_project.exception.InvalidDataException; import hamsung.hamsung_project.repository.*; import hamsung.hamsung_project.repository.RecruitsRepository; import hamsung.hamsung_project.repository.StudyMemberRepository; @@ -28,6 +29,7 @@ public class RecruitsService { private final UserRepository userRepository; private final StudyMemberService studyMemberService; private final CommentRepository commentRepository; + private final ChildCommentRepository childCommentRepository; //게시글 생성 //스터디 생성 @Transactional @@ -108,13 +110,15 @@ public boolean deleteRecruit(Long id){ Recruit target=recruitsRepository.findById(id).orElseThrow(()->new IllegalArgumentException("해당 게시글을 찾을 수 없습니다.")); if (recruitsRepository.existsById(id)) { //FK 연관관계 때문에 게시글에 딸린 comments 먼저 삭제. - List comment=commentRepository.findAllByRecruitId(id); - commentRepository.deleteAll(comment); +// List comment=commentRepository.findAllByRecruitId(id); +// commentRepository.deleteAll(comment); + recruitsRepository.deleteById(id); return true; } else { return false; } + } //스터디 모집 상태 변경(버튼 클릭?)