Spring Data JPA Query Methods

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

We have learned about creating a simple hello world example in previous article. In this article we will learn about retrieving data from entities without really writing queries.


Spring Data JPA Query Methods:


As mentioned in Spring Data JPA Hello World article, we will need to have things set up in eclipse. It would be redundant to write down all steps for each article, so please follow required steps in hello world article.

Why use spring data jpa query methods?

Using only JPA, if we want to retrieve data, we had to write JPQL, which needs some basic knowledge. Now with spring data jpa query methods, you need not write jpql queries for simple retrievals. For complex retrievals you may have to queries but simple things definitely become simpler with spring data jpa query methods.

Let us get started with example


Employee Repository:

Things to notice here:

  • findByDepartment(String department) : finds employees by the department name passed as parameters
  • findBySalaryLessThan(Long salary): find employees by salary less than specified as parameter.
  • findByDepartmentAndSalaryLessThan(String dept, Long salary): find employees by department passed as first parameter and salary less than value specified as second parameter.
  • spring data jpa query methods automatically covert methods into queries.
  • Table below is directly taken from spring data jpa docs
Keyword Sample JPQL snippet
And findByLastnameAndFirstname … where x.lastname = ?1 and x.firstname = ?2
Or findByLastnameOrFirstname … where x.lastname = ?1 or x.firstname = ?2
Is,Equals findByFirstname,



… 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 findByAgeIsNull … 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<Age> ages) … where x.age in ?1
NotIn findByAgeNotIn(Collection<Age> age) … 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)


Let us try to execute our code

Running above program will output below

I hope the article helped introduce spring data jpa query methods

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

3 thoughts on “Spring Data JPA Query Methods

  • Pingback:Sorting in Spring Data JPA - theJavaGeek

  • June 21, 2018 at 12:47 am

    What about a method name that would be similar to;

    Select * from employee e where e.lastname Like ‘[?1 – ?2]%’

    basically giving all employess whose last name starts with the letters between the 2 parameters?
    I can’t seem to get the method right and the @Query string can not get result set.


Leave a Reply

Your email address will not be published. Required fields are marked *