Spring

Spring - JPA findBy findAllBy 차이점

codeManager 2024. 3. 27. 23:04
반응형

 

JPA를 쓰다보면 findBy를 써야할지 findAllBy를 써야할지 고민이 생기는 경우가 있습니다.

 

findBy와 findAllBy는 어떤 차이가 있을까요?

 

정답은 findBy와 findAllBy는 동일하게 작동합니다.

 

List<SomeEntity> findBySomeCondition();

 

List<SomeEntity> findAllBySomeCondition();

 

 

findBy와 findAllBy는 동일한 쿼리가 수행됩니다.

 

 

메서드에서 쿼리를 파생할 때 Spring Data에서는 All 부분을 무시됩니다. 

 

중요한 부분은 By 뒤에 오는 키워드이며, 그 뒤에 오는 모든 것은 필드 이름으로 취급됩니다.

 

 

Any text between find (or other introducing keywords) and By is considered to be descriptive unless using one of the result-limiting keywords such as a Distinct to set a distinct flag on the query to be created or Top/First to limit query results.

 

 

반응형