Skip to content

Commit

Permalink
[BE] refactor: NativeQuery JPQL로 변환 및 value 파라미터 생략 (#808)
Browse files Browse the repository at this point in the history
  • Loading branch information
donghoony authored Oct 11, 2024
1 parent 9a9d6de commit b6d03d1
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ 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)
@Query("""
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 @@ -12,17 +12,17 @@ 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)
@Query("""
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)
@Query("""
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 @@ -11,27 +11,27 @@
@Repository
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)
@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<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)
@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<Question> findAllByTemplatedId(long templateId);

@Query(value = """
@Query("""
SELECT q FROM Question q
JOIN SectionQuestion sq ON q.id = sq.questionId
WHERE sq.sectionId = :sectionId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@Repository
public interface AnswerRepository extends JpaRepository<Answer, Long> {

@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
Expand All @@ -20,23 +20,23 @@ public interface AnswerRepository extends JpaRepository<Answer, Long> {
""")
List<Answer> findReceivedAnswersByQuestionIds(long reviewGroupId, Collection<Long> questionIds, int limit);

@Query(value = """
@Query("""
SELECT a.id FROM Answer a
JOIN Review r
ON a.reviewId = r.id
WHERE r.reviewGroupId = :reviewGroupId
""")
Set<Long> findIdsByReviewGroupId(long reviewGroupId);

@Query(value = """
@Query("""
SELECT a FROM Answer a
JOIN Review r
ON a.reviewId = r.id
WHERE r.reviewGroupId = :reviewGroupId
""")
Set<Answer> findAllByReviewGroupId(long reviewGroupId);

@Query(value = """
@Query("""
SELECT a.id FROM Answer a
WHERE a.questionId = :questionId
""")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,31 @@

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)
@Query("""
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
@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<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
@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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
@Repository
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
@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<Section> findAllByTemplateId(long templateId);

@Query("""
Expand Down

0 comments on commit b6d03d1

Please sign in to comment.