Spring
스파르타 부트캠프 Query Method (다락방)
H.S-Backend
2024. 6. 18. 16:47
JPA Query Method는 메서드 이름을 통해서 쿼리를 자동 생성한다.
메서드 이름은
특정 키워드와 규칙을 따르고
Spring Data JPA는
이를 해석하여 적절한
JPQL(Java Persistence Query Language)쿼리를 생성한다.
메소드 생성규칙
(find, delete, count, exists) By + 접두사 - 키워드 - 필드이름으로
시작해야한다.
Optional<Entity> findByField(Objects fieldName);
void deleteByField(Objects fieldName);
long countByField(Objects fieldName);
boolean existsByField(Objects fieldName)
Keyword | Sample | 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 is 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<Age> ages) | … where x.age in ?1 |
NotIn | findByAgeNotIn(Collection<Age> ages) | … where x.age not in ?1 |
True | findByActiveTrue() | … where x.active = true |
False | findByActiveFalse() | … where x.active = false |
https://hs-backend.tistory.com/203
스파르타 부트캠프 연관관계 맵핑 (다락방)
객체지향 설계의 목표는 자율적인 객체들의 협력 공동체를 만드는 것이다. 기존의 데이터베이스에서는 외래키를 사용하지만JPA에서는 객체를 참조하는 방식으로 연관관계를 매핑할 수 있다.
hs-backend.tistory.com
반응형