JPA - 쿼리 메소드 (Query Method)

mangez_js·2024년 6월 4일

Study

목록 보기
2/47

쿼리 메소드(Query Method)

Spring Data JPA에서 제공하는 핵심 기능 중 하나로 Repository 인터페이스에 간단한 네이밍 룰에 따라 메소드를 작성하면 원하는 쿼리를 실행하도록 지원하는 메소드

쿼리메소드 문법

  • find 문법
    find + (엔티티 이름) + By + 변수이름
  1. save(S entity) : 엔티티를 저장하거나 업데이트 합니다.
  2. findById(ID id) : 주어진 ID에 해당하는 엔티티를 검색합니다.
  3. existById(ID id) : 주어진 ID에 해당하는 엔티티의 존재 여부를 확인합니다.
  4. findAll() : 모든 엔티티를 검색합니다.
  5. findAllById(Iterable<ID> ids) : 주어진 ID 목록에 해당하는 모든 엔티티를 검색합니다.
  6. count() : 저장된 엔티티의 총 수를 반환합니다.
  7. deleteById(ID id) : 주어진 ID에 해당하는 엔티티를 삭제합니다.
  8. delete(T entity) : 주어진 엔티티를 삭제합니다.
  9. deleteAll() : 모든 엔티티를 삭제합니다.

쿼리 메소드 Sample 및 JPQL snippet

KeywordSampleJPQL snippet
DistinctfindDistincetByLastnameAndFirstnameselect distinct ... where x.lastname = ?1 and x.firstname = ?2
AndfindByLastnameAndFirstname… where x.lastname = ?1 and x.firstname = ?2
OrfindByLastnameOrFirstname… where x.lastname = ?1 or x.firstname = ?2
Is、EqualsfindByFirstname, findByFirstnameIs, findByFirstnameEquals… where x.firstname = ?1
BetweenfindByStartDateBetween… where x.startDate between ?1 and ?2
LessThanfindByAgeLessThan… where x.age < ?1
LessThanEqualfindByAgeLessThanEqual… where x.age <= ?1
GreaterThanfindByAgeGreaterThan… where x.age > ?1
GreaterThanEqualfindByAgeGreaterThanEqual… where x.age >= ?1
AfterfindByStartDateAfter… where x.startDate > ?1
BeforefindByStartDateBefore… where x.startDate < ?1
IsNull、 NullfindByAge(Is)Null… where x.age is null
IsNotNull、 NotNullfindByAge(Is)NotNull… where x.age not null
LikefindByFirstnameLike… where x.firstname like ?1
NotLikefindByFirstnameNotLike… where x.firstname not like ?1
StartingWithfindByFirstnameStartingWith… where x.firstname like ?1 ( %가 뒤에 추가된 매개 변수)
EndingWithfindByFirstnameEndingWith… where x.firstname like ?1 ( %가 앞에 추가된 매개 변수)
ContainingfindByFirstnameContaining… where x.firstname like ?1 ( %가 래핑된 매개 변수)
OrderByfindByAgeOrderByLastnameDesc… where x.age = ?1 order by x.lastname desc
NotfindByLastnameNot… where x.lastname <> ?1
InfindByAgeIn(Collection ages)… where x.age in ?1
NotInfindByAgeNotIn(Collection ages… where x.age not in ?1
TruefindByActiveTrue()… where x.active = true
FalsefindByActiveFalse()… where x.active = false
IgnoreCasefindByFirstnameIgnoreCase… where UPPER(x.firstname) = UPPER(?1)

0개의 댓글