Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] refactor: fetchtype을 Lazy로 변경한 후 EntityGraph를 사용해 최적화 #978

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading