JDBC, Transactions, Spring Data JPA
July 31, 2021

M03 Q27 What is the naming convention for finder methods in a Repository interface?

limit – result of the query can be limited by usage of first/top keyword

  • findFirst10ByLastname
  • findFirstByOrderByLastnameAsc
  • findTop3ByLastname
  • findTopByOrderByAgeDesc

property/properties expression – result will be filtered based on property of entity, multiple properties can be used with usage of And, Or keyword

  • findByLastnameAndFirstname
  • findByLastnameOrFirstname
  • findByFirstname

comparison – comparison mode can be specified after specifying property used for filtering

  • findByFirstnameIs
  • findByFirstnameEquals
  • findByStartDateBetween
  • findByAgeLessThan, findByAgeLessThanEqual
  • findByAgeGreaterThan, findByAgeGreaterThanEqual
  • findByStartDateBefore, findByStartDateAfter
  • findByAgeIsNull, findByAgeIsNotNull
  • findByFirstnameLike, findByFirstnameNotLike
  • findByFirstnameStartingWith, findByFirstnameEndingWith
  • findByFirstnameContaining
  • findByLastnameNot
  • findByAgeIn(Collection ages), findByAgeNotIn(Collection ages)
  • findByActiveTrue, findByActiveFalse
  • findByFirstnameIgnoreCase

ordering operator – optionally you can specify ordering operator at the end of method name

  • findByLastnameOrderByFirstnameAsc
  • findByLastnameOrderByFirstnameDesc