org.springframework.dao.InvalidDataAccessApiUsageException: For queries with named parameters you need to provide names for method parameters;

한지연·2023년 12월 3일

❗️ 문제 상황

  • 코드

    @Lock(LockModeType.PESSIMISTIC_WRITE)
    @Query(value = "select s from Stock s where s.id = :id")
    Stock findByIdWithPessimisticLock(Long id);
  • exception
org.springframework.dao.InvalidDataAccessApiUsageException: For queries with named parameters you need to provide names for method parameters; Use @Param for query method parameters, or when on Java 8+ use the javac flag -parameters
.....

🔍 문제 원인

쿼리에 쓸 파라메터를 정확하게 지명해주지 않은 것이 문제였다.


✅ 해결 방법

자바도 친절하게 해결 방법을 알려준다. 그리고 인프런에서도 해답을 찾았다.

  • 수정한 코드
    @Lock(LockModeType.PESSIMISTIC_WRITE)
    @Query(value = "select s from Stock s where s.id = :id")
    Stock findByIdWithPessimisticLock(@Param("id") Long id);

🗝️ : parameter 앞에 @Param 어노테이션을 사용하여 해결해준다.

profile
배우고 활용하는 것을 즐기는 개발자, 한지연입니다!

0개의 댓글