Skip to content

Commit

Permalink
refactor: NativeQuery를 JPQL로 변환
Browse files Browse the repository at this point in the history
  • Loading branch information
donghoony committed Oct 10, 2024
1 parent 1c5b177 commit 61e639a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public interface OptionGroupRepository extends JpaRepository<OptionGroup, Long>
Optional<OptionGroup> findByQuestionId(long questionId);

@Query(value = """
SELECT og.* FROM option_group og
WHERE og.question_id IN (:questionIds)
""", nativeQuery = true)
SELECT og FROM OptionGroup og
WHERE og.questionId IN (:questionIds)
""")
List<OptionGroup> findAllByQuestionIds(List<Long> questionIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ public interface OptionItemRepository extends JpaRepository<OptionItem, Long> {
List<OptionItem> findAllByOptionGroupId(long optionGroupId);

@Query(value = """
SELECT o.* FROM option_item o
WHERE o.option_type = :#{#optionType.name()}
""", nativeQuery = true)
SELECT o FROM OptionItem o
WHERE o.optionType = :optionType
""")
List<OptionItem> 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)
SELECT o FROM OptionItem o
JOIN OptionGroup og
ON o.optionGroupId = og.id
WHERE og.questionId IN :questionIds
""")
List<OptionItem> findAllByQuestionIds(List<Long> questionIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@
public interface QuestionRepository extends JpaRepository<Question, Long> {

@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)
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<Long> 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)
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<Question> findAllByTemplatedId(long templateId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,28 @@
public interface ReviewRepository extends JpaRepository<Review, Long> {

@Query(value = """
SELECT r.* FROM new_review r
WHERE r.review_group_id = :reviewGroupId
ORDER BY r.created_at DESC
""", nativeQuery = true)
SELECT r FROM Review r
WHERE r.reviewGroupId = :reviewGroupId
ORDER BY r.createdAt DESC
""")
List<Review> findAllByGroupId(long reviewGroupId);

@Query(value = """
SELECT r.* FROM new_review r
WHERE r.review_group_id = :reviewGroupId
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<Review> findByReviewGroupIdWithLimit(long reviewGroupId, Long lastReviewId, int limit);

Optional<Review> findByIdAndReviewGroupId(long reviewId, long reviewGroupId);

@Query(value = """
SELECT COUNT(r.id) FROM new_review r
WHERE r.review_group_id = :reviewGroupId
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
public interface SectionRepository extends JpaRepository<Section, Long> {

@Query(value = """
SELECT s.* FROM section s
JOIN template_section ts
ON s.id = ts.section_id
WHERE ts.template_id = :templateId
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<Section> findAllByTemplateId(long templateId);
}

0 comments on commit 61e639a

Please sign in to comment.