이 섹션은 Spring Data JPA에서 쿼리를 생성하는 여러가지 방법을 설명합니다
JPA 모듈은 쿼리를 문자열로 수동 정의하거나 메서드 이름에서 파생되도록 지원합니다.
Derived queries with the predicates IsStartingWith, StartingWith, StartsWith, IsEndingWith, EndingWith, EndsWith, IsNotContaining, NotContaining, NotContains, IsContaining, Containing, Contains the respective arguments for these queries will get sanitized. 즉,인수에 실제로 LIKE와일드 카드로 인식되는 문자가 포함되어 있으면 escape처리되어 literal로만 일치합니다. 사용되는 escape문자 escapeCharacter는 @EnableJpaRepository주석을 설정하여 구성 할 수 있습니다. SpEL 표현식 사용과 비교하세요.
메서드 이름에서 파생 된 쿼리를 가져오는 것은 매우 편리하지만 메서드 이름 파서가 사용하려는 키워드를 지원하지 않거나 메서드 이름이 불필요하게 추악해지는 상황에서 직면 할 수 있습니다. 따라서 명명 규칙을 통해 JPA명명 된 쿼리를 사용하거나(자세한 내용은 JPA명명 된 쿼리 사용 참조) 쿼리 메서드에 주석을 추가 할 수 있습니다. @Query(자세한 내용은 사용 ```@Query 참조).
일반적으로 JPA에 대한 쿼리 생성 메커니즘은“[repositories.query-methods]” 에 설명 된대로 작동합니다. 다음 예제는 JPA 쿼리 메소드가 무엇으로 변환되는지 보여줍니다.
Example 4. Query creation from method names(메서드 이름에서 쿼리생성)
public interface UserRepository extends Repository<User, Long> { List<User> findByEmailAddressAndLastname(String emailAddress, String lastname); }여기에서 JPA 기준 API를 사용하여 쿼리를 생성하지만 기본적으로 이것은 다음 쿼리로 변환됩니다 select u from User u where u.emailAddress = ?1 and u.lastname = ?2.. SpringData JPA는“ [repositories.query-methods.query-property-expressions] ”에 설명 된대로 속성 검사를 수행하고 중첩 속성을 탐색 합니다 .
다음 표는 JPA에 대해 지원되는 키워드와 해당 키워드를 포함하는 메소드가 변환하는 대상을 설명합니다.
Table 2. 메소드 이름 내에서 지원되는 키워드
| Keyword | Sample | JPQL snippet |
|---|---|---|
| Distinct | findDistinctByLastnameAndFirstname | select distinct … where x.lastname = ?1 and x.firstname = ?2 |
| And | findByLastnameAndFirstname | … where x.lastname = ?1 and x.firstname = ?2 |
| Or | findByLastnameOrFirstname | … where x.lastname = ?1 or x.firstname = ?2 |
| Is, Equals | findByFirstname,findByFirstnameIs,findByFirstnameEquals | … where x.firstname = ?1 |
| Between | findByStartDateBetween | … where x.startDate between ?1 and ?2 |
| LessThan | findByAgeLessThan | … where x.age < ?1 |
| LessThanEqual | findByAgeLessThanEqual | … where x.age <= ?1 |
| GreaterThan | findByAgeGreaterThan | … where x.age > ?1 |
| GreaterThanEqual | findByAgeGreaterThanEqual | … where x.age >= ?1 |
| After | findByStartDateAfter | … where x.startDate > ?1 |
| Before | findByStartDateBefore | … where x.startDate < ?1 |
| IsNull, Null | findByAge(Is)Null | … where x.age is null |
| IsNotNull, NotNull | findByAge(Is)NotNull | … where x.age not null |
| Like | findByFirstnameLike | … where x.firstname like ?1 |
| NotLike | findByFirstnameNotLike | … where x.firstname not like ?1 |
| StartingWith | findByFirstnameStartingWith | … where x.firstname like ?1 (parameter bound with appended %) |
| EndingWith | findByFirstnameEndingWith | … where x.firstname like ?1 (parameter bound with prepended %) |
| Containing | findByFirstnameContaining | … where x.firstname like ?1 (parameter bound wrapped in %) |
| OrderBy | findByAgeOrderByLastnameDesc | … where x.age = ?1 order by x.lastname desc |
| Not | findByLastnameNot | … where x.lastname <> ?1 |
| In | findByAgeIn(Collection ages) | … where x.age in ?1 |
| NotIn | findByAgeNotIn(Collection ages) | … where x.age not in ?1 |
| True | findByActiveTrue() | … where x.active = true |
| False | findByActiveFalse() | … where x.active = false |
| IgnoreCase | findByFirstnameIgnoreCase | … where UPPER(x.firstame) = UPPER(?1) |