From f628bbbd00e166fe9fea6633c194a4be3e371447 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=95=EC=A0=95=EB=AF=BC?= Date: Fri, 9 Aug 2024 15:17:28 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20on=20delete=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/edufocus/edufocus/qna/entity/Qna.java | 4 ++++ .../com/edufocus/edufocus/report/entity/vo/Report.java | 7 +++++-- .../edufocus/edufocus/report/entity/vo/ReportSet.java | 9 ++++----- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/backend/src/main/java/com/edufocus/edufocus/qna/entity/Qna.java b/backend/src/main/java/com/edufocus/edufocus/qna/entity/Qna.java index af797ee..15ea83a 100644 --- a/backend/src/main/java/com/edufocus/edufocus/qna/entity/Qna.java +++ b/backend/src/main/java/com/edufocus/edufocus/qna/entity/Qna.java @@ -4,6 +4,8 @@ import com.edufocus.edufocus.lecture.entity.Lecture; import com.edufocus.edufocus.user.model.entity.vo.User; import jakarta.persistence.*; import lombok.*; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; import java.util.Date; @@ -13,6 +15,8 @@ import java.util.Date; @Builder @NoArgsConstructor @AllArgsConstructor + +@OnDelete(action = OnDeleteAction.CASCADE) public class Qna { // 연관관계 주인 diff --git a/backend/src/main/java/com/edufocus/edufocus/report/entity/vo/Report.java b/backend/src/main/java/com/edufocus/edufocus/report/entity/vo/Report.java index df47ac2..0ac149b 100644 --- a/backend/src/main/java/com/edufocus/edufocus/report/entity/vo/Report.java +++ b/backend/src/main/java/com/edufocus/edufocus/report/entity/vo/Report.java @@ -7,6 +7,8 @@ import com.fasterxml.jackson.annotation.JsonAutoDetect; import jakarta.persistence.*; import lombok.*; import org.hibernate.annotations.CreationTimestamp; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; @@ -22,6 +24,7 @@ import java.util.List; @Builder @EntityListeners(AuditingEntityListener.class) @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY) +@OnDelete(action = OnDeleteAction.CASCADE) public class Report { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -50,10 +53,10 @@ public class Report { @JoinColumn(name = "reportset_id") private ReportSet reportSet; - @OneToMany(mappedBy = "report", cascade = CascadeType.REMOVE) + @OneToMany(mappedBy = "report", cascade = CascadeType.REMOVE) private List answers; - public ReportResponse makeReportResponse(){ + public ReportResponse makeReportResponse() { return ReportResponse.builder() .reportId(id) .name(user.getName()) diff --git a/backend/src/main/java/com/edufocus/edufocus/report/entity/vo/ReportSet.java b/backend/src/main/java/com/edufocus/edufocus/report/entity/vo/ReportSet.java index fb293ec..1fd87e1 100644 --- a/backend/src/main/java/com/edufocus/edufocus/report/entity/vo/ReportSet.java +++ b/backend/src/main/java/com/edufocus/edufocus/report/entity/vo/ReportSet.java @@ -5,10 +5,9 @@ import com.edufocus.edufocus.quiz.entity.QuizSet; import com.edufocus.edufocus.report.entity.dto.ReportSetResponse; import com.fasterxml.jackson.annotation.JsonAutoDetect; import jakarta.persistence.*; +import jakarta.persistence.CascadeType; import lombok.*; -import org.hibernate.annotations.ColumnDefault; -import org.hibernate.annotations.CreationTimestamp; -import org.hibernate.annotations.GenericGenerator; +import org.hibernate.annotations.*; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; @@ -19,7 +18,6 @@ import java.util.List; import java.util.UUID; - @Entity @Getter @Builder @@ -27,6 +25,7 @@ import java.util.UUID; @AllArgsConstructor @EntityListeners(AuditingEntityListener.class) @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY) +@OnDelete(action = OnDeleteAction.CASCADE) public class ReportSet { @Id @GeneratedValue(generator = "UUID") @@ -60,7 +59,7 @@ public class ReportSet { .build(); } - public long findUserId(){ + public long findUserId() { return lecture.getUser().getId(); } }