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: NativeQuery를 JPQL로 변환 #808

Merged
merged 1 commit into from
Oct 11, 2024
Merged
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 @@ -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