From b6d03d1481a7cf7176f573ae459c6582fa2d5697 Mon Sep 17 00:00:00 2001 From: Donghoon Lee Date: Fri, 11 Oct 2024 17:38:16 +0900 Subject: [PATCH] =?UTF-8?q?[BE]=20refactor:=20NativeQuery=20JPQL=EB=A1=9C?= =?UTF-8?q?=20=EB=B3=80=ED=99=98=20=EB=B0=8F=20`value`=20=ED=8C=8C?= =?UTF-8?q?=EB=9D=BC=EB=AF=B8=ED=84=B0=20=EC=83=9D=EB=9E=B5=20(#808)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/OptionGroupRepository.java | 8 ++--- .../repository/OptionItemRepository.java | 20 +++++------ .../repository/QuestionRepository.java | 34 +++++++++--------- .../review/repository/AnswerRepository.java | 8 ++--- .../review/repository/ReviewRepository.java | 36 +++++++++---------- .../repository/SectionRepository.java | 12 +++---- 6 files changed, 57 insertions(+), 61 deletions(-) diff --git a/backend/src/main/java/reviewme/question/repository/OptionGroupRepository.java b/backend/src/main/java/reviewme/question/repository/OptionGroupRepository.java index 3935d6a8f..ad2994537 100644 --- a/backend/src/main/java/reviewme/question/repository/OptionGroupRepository.java +++ b/backend/src/main/java/reviewme/question/repository/OptionGroupRepository.java @@ -12,9 +12,9 @@ public interface OptionGroupRepository extends JpaRepository Optional findByQuestionId(long questionId); - @Query(value = """ - SELECT og.* FROM option_group og - WHERE og.question_id IN (:questionIds) - """, nativeQuery = true) + @Query(""" + SELECT og FROM OptionGroup og + WHERE og.questionId IN :questionIds + """) List findAllByQuestionIds(List questionIds); } diff --git a/backend/src/main/java/reviewme/question/repository/OptionItemRepository.java b/backend/src/main/java/reviewme/question/repository/OptionItemRepository.java index 6466aa0bf..e42274c33 100644 --- a/backend/src/main/java/reviewme/question/repository/OptionItemRepository.java +++ b/backend/src/main/java/reviewme/question/repository/OptionItemRepository.java @@ -12,17 +12,17 @@ public interface OptionItemRepository extends JpaRepository { List findAllByOptionGroupId(long optionGroupId); - @Query(value = """ - SELECT o.* FROM option_item o - WHERE o.option_type = :#{#optionType.name()} - """, nativeQuery = true) + @Query(""" + SELECT o FROM OptionItem o + WHERE o.optionType = :optionType + """) List findAllByOptionType(OptionType optionType); - @Query(value = """ - SELECT o.* FROM option_item o - JOIN option_group og - ON o.option_group_id = og.id - WHERE og.question_id IN (:questionIds) - """, nativeQuery = true) + @Query(""" + SELECT o FROM OptionItem o + JOIN OptionGroup og + ON o.optionGroupId = og.id + WHERE og.questionId IN :questionIds + """) List findAllByQuestionIds(List questionIds); } diff --git a/backend/src/main/java/reviewme/question/repository/QuestionRepository.java b/backend/src/main/java/reviewme/question/repository/QuestionRepository.java index 889202bf3..9db137d25 100644 --- a/backend/src/main/java/reviewme/question/repository/QuestionRepository.java +++ b/backend/src/main/java/reviewme/question/repository/QuestionRepository.java @@ -11,27 +11,27 @@ @Repository public interface QuestionRepository extends JpaRepository { - @Query(value = """ - SELECT q.id FROM question q - JOIN section_question sq - ON q.id = sq.question_id - JOIN template_section ts - ON sq.section_id = ts.section_id - WHERE ts.template_id = :templateId - """, nativeQuery = true) + @Query(""" + SELECT q.id FROM Question q + JOIN SectionQuestion sq + ON q.id = sq.questionId + JOIN TemplateSection ts + ON sq.sectionId = ts.sectionId + WHERE ts.templateId = :templateId + """) Set findAllQuestionIdByTemplateId(long templateId); - @Query(value = """ - SELECT q.* FROM question q - JOIN section_question sq - ON q.id = sq.question_id - JOIN template_section ts - ON sq.section_id = ts.section_id - WHERE ts.template_id = :templateId - """, nativeQuery = true) + @Query(""" + SELECT q FROM Question q + JOIN SectionQuestion sq + ON q.id = sq.questionId + JOIN TemplateSection ts + ON sq.sectionId = ts.sectionId + WHERE ts.templateId = :templateId + """) List findAllByTemplatedId(long templateId); - @Query(value = """ + @Query(""" SELECT q FROM Question q JOIN SectionQuestion sq ON q.id = sq.questionId WHERE sq.sectionId = :sectionId diff --git a/backend/src/main/java/reviewme/review/repository/AnswerRepository.java b/backend/src/main/java/reviewme/review/repository/AnswerRepository.java index 821fbb7cc..d296f89fe 100644 --- a/backend/src/main/java/reviewme/review/repository/AnswerRepository.java +++ b/backend/src/main/java/reviewme/review/repository/AnswerRepository.java @@ -11,7 +11,7 @@ @Repository public interface AnswerRepository extends JpaRepository { - @Query(value = """ + @Query(""" SELECT a FROM Answer a JOIN Review r ON a.reviewId = r.id WHERE r.reviewGroupId = :reviewGroupId AND a.questionId IN :questionIds @@ -20,7 +20,7 @@ public interface AnswerRepository extends JpaRepository { """) List findReceivedAnswersByQuestionIds(long reviewGroupId, Collection questionIds, int limit); - @Query(value = """ + @Query(""" SELECT a.id FROM Answer a JOIN Review r ON a.reviewId = r.id @@ -28,7 +28,7 @@ public interface AnswerRepository extends JpaRepository { """) Set findIdsByReviewGroupId(long reviewGroupId); - @Query(value = """ + @Query(""" SELECT a FROM Answer a JOIN Review r ON a.reviewId = r.id @@ -36,7 +36,7 @@ public interface AnswerRepository extends JpaRepository { """) Set findAllByReviewGroupId(long reviewGroupId); - @Query(value = """ + @Query(""" SELECT a.id FROM Answer a WHERE a.questionId = :questionId """) diff --git a/backend/src/main/java/reviewme/review/repository/ReviewRepository.java b/backend/src/main/java/reviewme/review/repository/ReviewRepository.java index bde88eeac..3a0600ad9 100644 --- a/backend/src/main/java/reviewme/review/repository/ReviewRepository.java +++ b/backend/src/main/java/reviewme/review/repository/ReviewRepository.java @@ -9,35 +9,31 @@ public interface ReviewRepository extends JpaRepository { - @Query(value = """ - SELECT r.* FROM new_review r - WHERE r.review_group_id = :reviewGroupId - ORDER BY r.created_at DESC - """, nativeQuery = true) + @Query(""" + SELECT r FROM Review r + WHERE r.reviewGroupId = :reviewGroupId + ORDER BY r.createdAt DESC + """) List findAllByGroupId(long reviewGroupId); - @Query(value = """ - SELECT r.* FROM new_review r - WHERE r.review_group_id = :reviewGroupId + @Query(""" + SELECT r FROM Review r + WHERE r.reviewGroupId = :reviewGroupId AND (:lastReviewId IS NULL OR r.id < :lastReviewId) - ORDER BY r.created_at DESC, r.id DESC + ORDER BY r.createdAt DESC, r.id DESC LIMIT :limit - """, nativeQuery = true) + """) List findByReviewGroupIdWithLimit(long reviewGroupId, Long lastReviewId, int limit); Optional findByIdAndReviewGroupId(long reviewId, long reviewGroupId); - @Query(value = """ - SELECT COUNT(r.id) FROM new_review r - WHERE r.review_group_id = :reviewGroupId + @Query(""" + SELECT COUNT(r.id) > 0 FROM Review r + WHERE r.reviewGroupId = :reviewGroupId AND r.id < :reviewId - AND CAST(r.created_at AS DATE) <= :createdDate - """, nativeQuery = true) - Long existsOlderReviewInGroupInLong(long reviewGroupId, long reviewId, LocalDate createdDate); - - default boolean existsOlderReviewInGroup(long reviewGroupId, long reviewId, LocalDate createdDate) { - return existsOlderReviewInGroupInLong(reviewGroupId, reviewId, createdDate) > 0; - } + AND CAST(r.createdAt AS java.time.LocalDate) <= :createdDate + """) + boolean existsOlderReviewInGroup(long reviewGroupId, long reviewId, LocalDate createdDate); int countByReviewGroupId(long reviewGroupId); } diff --git a/backend/src/main/java/reviewme/template/repository/SectionRepository.java b/backend/src/main/java/reviewme/template/repository/SectionRepository.java index ae0aff9f6..d40fa5a24 100644 --- a/backend/src/main/java/reviewme/template/repository/SectionRepository.java +++ b/backend/src/main/java/reviewme/template/repository/SectionRepository.java @@ -10,13 +10,13 @@ @Repository public interface SectionRepository extends JpaRepository { - @Query(value = """ - SELECT s.* FROM section s - JOIN template_section ts - ON s.id = ts.section_id - WHERE ts.template_id = :templateId + @Query(""" + SELECT s FROM Section s + JOIN TemplateSection ts + ON s.id = ts.sectionId + WHERE ts.templateId = :templateId ORDER BY s.position ASC - """, nativeQuery = true) + """) List
findAllByTemplateId(long templateId); @Query("""