diff --git a/backend/src/main/java/reviewme/review/domain/CheckboxAnswer.java b/backend/src/main/java/reviewme/review/domain/CheckboxAnswer.java index 6171b9fc5..ca67638b0 100644 --- a/backend/src/main/java/reviewme/review/domain/CheckboxAnswer.java +++ b/backend/src/main/java/reviewme/review/domain/CheckboxAnswer.java @@ -20,7 +20,7 @@ @Getter public class CheckboxAnswer extends Answer { - @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) + @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true) @JoinColumn(name = "checkbox_answer_id", nullable = false, updatable = false) private List selectedOptionIds; diff --git a/backend/src/main/java/reviewme/review/domain/Review.java b/backend/src/main/java/reviewme/review/domain/Review.java index aa540e57b..cceac4a49 100644 --- a/backend/src/main/java/reviewme/review/domain/Review.java +++ b/backend/src/main/java/reviewme/review/domain/Review.java @@ -37,7 +37,7 @@ public class Review { @Column(name = "review_group_id", nullable = false) private long reviewGroupId; - @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST) + @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST) @JoinColumn(name = "review_id", nullable = false, updatable = false) private List answers; @@ -61,6 +61,7 @@ public boolean hasAnsweredQuestion(long questionId) { return getAnsweredQuestionIds().contains(questionId); } + // 하위 타입의 구체적인 정보를 알지 못하므로, fetch join할 수 없다..? public List getAnswersByType(Class clazz) { return answers.stream() .filter(clazz::isInstance) diff --git a/backend/src/main/java/reviewme/review/repository/ReviewRepository.java b/backend/src/main/java/reviewme/review/repository/ReviewRepository.java index 3a0600ad9..c9f941ca7 100644 --- a/backend/src/main/java/reviewme/review/repository/ReviewRepository.java +++ b/backend/src/main/java/reviewme/review/repository/ReviewRepository.java @@ -3,12 +3,14 @@ import java.time.LocalDate; import java.util.List; import java.util.Optional; +import org.springframework.data.jpa.repository.EntityGraph; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import reviewme.review.domain.Review; public interface ReviewRepository extends JpaRepository { + @EntityGraph(attributePaths = {"answers"}) @Query(""" SELECT r FROM Review r WHERE r.reviewGroupId = :reviewGroupId @@ -16,6 +18,7 @@ public interface ReviewRepository extends JpaRepository { """) List findAllByGroupId(long reviewGroupId); + @EntityGraph(attributePaths = {"answers"}) @Query(""" SELECT r FROM Review r WHERE r.reviewGroupId = :reviewGroupId @@ -25,6 +28,7 @@ public interface ReviewRepository extends JpaRepository { """) List findByReviewGroupIdWithLimit(long reviewGroupId, Long lastReviewId, int limit); + @EntityGraph(attributePaths = {"answers"}) Optional findByIdAndReviewGroupId(long reviewId, long reviewGroupId); @Query(""" diff --git a/backend/src/main/java/reviewme/template/domain/Section.java b/backend/src/main/java/reviewme/template/domain/Section.java index b9fa82b16..ef84f9e08 100644 --- a/backend/src/main/java/reviewme/template/domain/Section.java +++ b/backend/src/main/java/reviewme/template/domain/Section.java @@ -34,7 +34,7 @@ public class Section { @Enumerated(EnumType.STRING) private VisibleType visibleType; - @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) + @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true) @JoinColumn(name = "section_id", nullable = false, updatable = false) private List questionIds; diff --git a/backend/src/main/java/reviewme/template/domain/Template.java b/backend/src/main/java/reviewme/template/domain/Template.java index 29b6f36d7..59bd8af12 100644 --- a/backend/src/main/java/reviewme/template/domain/Template.java +++ b/backend/src/main/java/reviewme/template/domain/Template.java @@ -26,7 +26,7 @@ public class Template { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) + @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true) @JoinColumn(name = "template_id", nullable = false, updatable = false) private List sectionIds; diff --git a/backend/src/main/java/reviewme/template/repository/SectionRepository.java b/backend/src/main/java/reviewme/template/repository/SectionRepository.java index d40fa5a24..6b6f50fdd 100644 --- a/backend/src/main/java/reviewme/template/repository/SectionRepository.java +++ b/backend/src/main/java/reviewme/template/repository/SectionRepository.java @@ -2,6 +2,7 @@ import java.util.List; import java.util.Optional; +import org.springframework.data.jpa.repository.EntityGraph; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; @@ -10,15 +11,16 @@ @Repository public interface SectionRepository extends JpaRepository { + @EntityGraph(attributePaths = {"questionIds"}) @Query(""" SELECT s FROM Section s - JOIN TemplateSection ts - ON s.id = ts.sectionId + JOIN TemplateSection ts ON s.id = ts.sectionId WHERE ts.templateId = :templateId ORDER BY s.position ASC """) List
findAllByTemplateId(long templateId); + @EntityGraph(attributePaths = {"questionIds"}) @Query(""" SELECT s FROM Section s JOIN TemplateSection ts ON s.id = ts.sectionId