Skip to content

Commit

Permalink
refactor: fetchtype을 Lazy로 변경한 후 EntityGraph를 사용해 최적화
Browse files Browse the repository at this point in the history
  • Loading branch information
donghoony committed Nov 20, 2024
1 parent 9b59f8d commit 58753d2
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<CheckboxAnswerSelectedOption> selectedOptionIds;

Expand Down
3 changes: 2 additions & 1 deletion backend/src/main/java/reviewme/review/domain/Review.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Answer> answers;

Expand All @@ -61,6 +61,7 @@ public boolean hasAnsweredQuestion(long questionId) {
return getAnsweredQuestionIds().contains(questionId);
}

// 하위 타입의 구체적인 정보를 알지 못하므로, fetch join할 수 없다..?
public <T extends Answer> List<T> getAnswersByType(Class<T> clazz) {
return answers.stream()
.filter(clazz::isInstance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
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<Review, Long> {

@EntityGraph(attributePaths = {"answers"})
@Query("""
SELECT r FROM Review r
WHERE r.reviewGroupId = :reviewGroupId
ORDER BY r.createdAt DESC
""")
List<Review> findAllByGroupId(long reviewGroupId);

@EntityGraph(attributePaths = {"answers"})
@Query("""
SELECT r FROM Review r
WHERE r.reviewGroupId = :reviewGroupId
Expand All @@ -25,6 +28,7 @@ public interface ReviewRepository extends JpaRepository<Review, Long> {
""")
List<Review> findByReviewGroupIdWithLimit(long reviewGroupId, Long lastReviewId, int limit);

@EntityGraph(attributePaths = {"answers"})
Optional<Review> findByIdAndReviewGroupId(long reviewId, long reviewGroupId);

@Query("""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SectionQuestion> questionIds;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TemplateSection> sectionIds;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -10,15 +11,16 @@
@Repository
public interface SectionRepository extends JpaRepository<Section, Long> {

@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<Section> findAllByTemplateId(long templateId);

@EntityGraph(attributePaths = {"questionIds"})
@Query("""
SELECT s FROM Section s
JOIN TemplateSection ts ON s.id = ts.sectionId
Expand Down

0 comments on commit 58753d2

Please sign in to comment.