Spring Boot Jpa Repository

Posted By : Tarun Jangra | 02-Feb-2022

JpaRepository is JPA specific extension of Repository . It contains the full API of CrudRepository and PagingAndSortingRepository . So it contains API for basic CRUD operations and also API for pagination and sorting. 

 

  • Structure of Derived Query Methods

The first part – like find – is the introducer and the rest – like ByName – is the criteria. Spring Data JPA supports find, read, query, count and get.

List<User> findByName(String name)

 

The criteria part contains the entity-specific condition expressions of the query. We can use the condition keywords along with the entity's property names. We can also concatenate the expressions with And and Or

List<User> findTop3ByAge()

----------------------------------------------------------------------------------------------------------------------------

  • Equality Condition Keywords

We can just append the property name without any keyword for an exact match condition

List<User> findByName(String name);

And we can add Is or Equals for readability

List<User> findByNameIs(String name);
List<User> findByNameEquals(String name);
List<User> findByNameIsNot(String name);

 

We can also use the IsNull keyword to add IS NULL criteria to the query

List<User> findByNameIsNull();
List<User> findByNameIsNotNull();


We can use True and False keywords to add equality conditions for boolean types:

List<User> findByActiveTrue();
List<User> findByActiveFalse();

----------------------------------------------------------------------------------------------------------------------------

  • Similarity Condition Keywords

We can find names that start with a value using StartingWith

List<User> findByNameStartingWith(String prefix);

 

If we want names that end with a value, then EndingWith is what we want

List<User> findByNameEndingWith(String suffix);

 

we can find which names contain a value with Containing

List<User> findByNameContaining(String infix);

 

we can add our own LIKE with the Like keyword

List<User> findByNameLike(String likePattern);

 

we can then hand in our LIKE pattern when we call the method

String likePattern = "a%b%c";
userRepository.findByNameLike(likePattern);

----------------------------------------------------------------------------------------------------------------------------

  • Comparison Condition Keywords

we can use LessThan and LessThanEqual keywords to compare the records with the given value using the and <= operators

List<User> findByAgeLessThan(Integer age);
List<User> findByAgeLessThanEqual(Integer age);

 

we can use GreaterThan and GreaterThanEqual keywords

List<User> findByAgeGreaterThan(Integer age);
List<User> findByAgeGreaterThanEqual(Integer age);

 

we can find users who are between two ages with Between

List<User> findByAgeBetween(Integer startAge, Integer endAge);

 

We can also supply a collection of ages to match against using In

List<User> findByAgeIn(Collection<Integer> ages);

 

we might want to query for users who were born before or after a given date

List<User> findByBirthDateAfter(ZonedDateTime birthDate);
List<User> findByBirthDateBefore(ZonedDateTime birthDate);

----------------------------------------------------------------------------------------------------------------------------

  • Multiple Condition Keywords

We can combine as many expressions as we need by using And and Or keywords

List<User> findByNameOrBirthDate(String name, ZonedDateTime birthDate);
List<User> findByNameOrBirthDateAndActive(String name, ZonedDateTime birthDate, Boolean active);

----------------------------------------------------------------------------------------------------------------------------

  • Sorting the Results

We could ask that the users be sorted alphabetically by their name using OrderBy

List<User> findByNameOrderByName(String name);
List<User> findByNameOrderByNameAsc(String name);

 

we can use Desc instead to sort them in reverse:

List<User> findByNameOrderByNameDesc(String name);

About Author

Author Image
Tarun Jangra

Tarun is an experienced Backend Developer with over 6 years of expertise in JAVA, DBMS, and building web services and CRM applications. He has technical skills in JAVA Core & Advance, Spring-Hibernate, Bootstrap, Apache, AWS Lambda Nifi, HTML/CSS3, jQuery, JavaScript and Visual Studio (VB.net), with knowledge of Java EE, Spring, and Hibernate frameworks. Additionally, Tarun has database skills in Oracle, SQL, MySQL, H2, Redis, MS-Access, and Postgres, and experience in web services such as SOAP and REST. Tarun has worked on various projects, including gharpeshiksha, CRM, Workbox, Tutorx, Pando, Punchin, and many more.

Request for Proposal

Name is required

Comment is required

Sending message..