From c848678bf97bbbe5f917a873b3a44ebe81b9b55e Mon Sep 17 00:00:00 2001 From: yeoEun Date: Sun, 9 Nov 2025 20:35:08 +0900 Subject: [PATCH] Fix/applicantCount --- .../chainee/repository/JobPostRepository.java | 18 ++++++++++++++++-- .../chainee/service/JobApplicationService.java | 2 +- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/chaineeproject/chainee/repository/JobPostRepository.java b/src/main/java/com/chaineeproject/chainee/repository/JobPostRepository.java index ba01ffe..76e3fb7 100644 --- a/src/main/java/com/chaineeproject/chainee/repository/JobPostRepository.java +++ b/src/main/java/com/chaineeproject/chainee/repository/JobPostRepository.java @@ -17,9 +17,23 @@ public interface JobPostRepository extends JpaRepository { @EntityGraph(attributePaths = {"author", "author.positions"}) Optional findById(Long id); + // πŸ” (μ‚­μ œ) 증뢄 방식 + // @Modifying(clearAutomatically = true, flushAutomatically = true) + // @Query("update JobPost p set p.applicantCount = p.applicantCount + 1 where p.id = :postId") + // int incrementApplicantCount(@Param("postId") Long postId); + + // βœ… 항상 μ‹€μ œ 건수둜 동기화(λ„€μ΄ν‹°λΈŒ 쿼리) @Modifying(clearAutomatically = true, flushAutomatically = true) - @Query("update JobPost p set p.applicantCount = p.applicantCount + 1 where p.id = :postId") - int incrementApplicantCount(Long postId); + @Query(value = """ + UPDATE job_post p + SET p.applicant_count = ( + SELECT COUNT(*) + FROM job_application a + WHERE a.post_id = :postId + ) + WHERE p.id = :postId + """, nativeQuery = true) + int syncApplicantCount(@Param("postId") Long postId); @Query(""" select jp diff --git a/src/main/java/com/chaineeproject/chainee/service/JobApplicationService.java b/src/main/java/com/chaineeproject/chainee/service/JobApplicationService.java index 2a2dbce..df89c53 100644 --- a/src/main/java/com/chaineeproject/chainee/service/JobApplicationService.java +++ b/src/main/java/com/chaineeproject/chainee/service/JobApplicationService.java @@ -52,7 +52,7 @@ public void applyToJob(Long postId, Long applicantId, Long resumeId) { application.setCreatedAt(LocalDateTime.now()); jobApplicationRepository.save(application); - jobPostRepository.incrementApplicantCount(postId); + jobPostRepository.syncApplicantCount(postId); // βœ… 지원 μ €μž₯ 직후 λ°”λ‘œ μ•Œλ¦Ό 생성 (이벀트/νŠΈλžœμž­μ…˜ ν›… 의쑴 X) Notification noti = Notification.builder()