JPA

[JPA] [Trouble Shooting] Use @Param for query method parameters, or when on Java 8+ use the javac flag -parameters 에러 해결

뚜코맨 2024. 6. 1. 19:29

팀 프로젝트를 하던 중에 팀원이 작업이 완료돼서 pull을 받고 실행을 돌려보았는데, 이런 오류를 마주치게 되었다.

 

 

오류원문

더보기

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

 

구글링을 해본 결과

 

레포지토리에서 JPQL을 사용한 부분에서 파라미터를 전달받을때 @Param("") 어노테이션을 쓰지 않고 파라미터를 받아와서 생긴 문제였다.

 

친절하게도 로그에 해결 방법을 알려주고 있다.

Use @Param for query method parameters, or when on Java 8+ use the javac flag -parameters.

  • @Param 어노테이션을 사용하거나 (Java 8+에서) -parameters 옵션을 사용하라는 것이다.

@Param이란 무엇이냐?

 

@Param: 매개변수의 네임을 통해 메서드의 매개 변수를 쿼리에 바인드 시키는 어노테이션

 

따라서 다음과 같이 이 에러를 해결할 수 있다.

 

@Param 어노테이션을 매개변수에 추가해서 해결을 할 수 있다.

 

그럼 두 번째 해결 방법인 javac flag -parameters 는 무엇을 나타내는 걸까?

 

Preferences(Command + ,) > Build, Execution, Deployment > Compiler > Java Compiler

Additional command line parameters 부분에 -parameters 를 추가해주면 해결이 된다.

 

그럼 -parameters가 무엇을 의미하는 것인가?

먼저 Spring Data JPA 공식문서 에서는 다음과 같이 나와있다.

As of version 4, Spring fully supports Java 8’s parameter name discovery based on the -parameters compiler flag. By using this flag in your build as an alternative to debug information, you can omit the @Param annotation for named parameters.

 

대략 -parameters compiler flag 를 통해 @Param 어노테이션을 생략할 수 있다고 한다.

 

그럼 이 설정을 키는 것이 마냥 도움이 되는 것일까?

구글링을 해보았더니 영한쌤의 답변을 찾을 수 있었다.

 

아직 이 옵션을 키지 않고 사용하는 경우가 많아 Spring Data JPA 공식문서에도 나와 있듯이, @Param() 어노테이션은 꼭 사용해달라고 한다.