In this tutorial, I will show you how to use JPA Repository to find by multiple fields example. At the end, you will know way to filter by multiple Columns using 3 ways: Derived Query, JPQL and Native Query.
This tutorial is originally from Bezkoder:
https://www.bezkoder.com/jpa-filter-by-multiple-columns/
Overview
For querying the data from the database with custom filter methods, Spring Data JPA support several ways:
- Derived Query: JPA creates SQL queries based on the finder method and execute the query behind the scenes.
List<Tutorial> findAll();
List<Tutorial> findByTitleContainingIgnoreCase(String title);
- JPQL: inspired by SQL. It resemble SQL queries in syntax, but operate against JPA entity objects stored in a relational database rather than directly with database tables.
@Query("SELECT t FROM Tutorial t")
List<Tutorial> findAll();
@Query("SELECT t FROM Tutorial t WHERE t.published=true")
List<Tutorial> findByPublished();
- Native Query: actual SQL queries which can be operated directly with database object and tables.
@Query(value = "SELECT * FROM tutorials", nativeQuery = true)
List<Tutorial> findAllNative();
@Query(value = "SELECT * FROM tutorials t WHERE t.published=true", nativeQuery = true)
List<Tutorial> findByPublishedNative();
For more details and examples about each kind, please visit:
- Spring JPA Derived Query example
- Spring JPA @Query example with JPQL
- Spring JPA Native Query example
Let's continue to create project that uses JPA Repository with find by multiple columns methods.
JPA filter by multiple columns
Technology
- Java 8
- Spring Boot 2 (with Spring Data JPA)
- MySQL/PostgreSQL/H2 (embedded database)
- Maven 3.8.1
Project Structure
Let me explain it briefly.
-
Tutorial
data model class correspond to entity and table tutorials. -
TutorialRepository
is an interface that extends JpaRepository for filter/findby methods. It will be autowired inSpringBootQueryExampleApplication
. -
SpringBootQueryExampleApplication
isSpringBootApplication
which implementsCommandLineRunner
. We will useTutorialRepository
to run Query methods here. - Configuration for Spring Datasource, JPA & Hibernate in application.properties.
- pom.xml contains dependencies for Spring Boot and MySQL/PostgreSQL/H2 database.
Create & Setup Spring Boot project
Use Spring web tool or your development tool (Spring Tool Suite, Eclipse, Intellij) to create a Spring Boot project.
Then open pom.xml and add these dependencies:
<!-- web for access H2 database UI -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
We also need to add one more dependency.
- If you want to use MySQL:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
- or PostgreSQL:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
- or H2 (embedded database):
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
Configure Spring Datasource, JPA, Hibernate
Under src/main/resources folder, open application.properties and write these lines.
- For MySQL:
spring.datasource.url= jdbc:mysql://localhost:3306/testdb?useSSL=false
spring.datasource.username= root
spring.datasource.password= 123456
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5InnoDBDialect
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto= update
- For PostgreSQL:
spring.datasource.url= jdbc:postgresql://localhost:5432/testdb
spring.datasource.username= postgres
spring.datasource.password= 123
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation= true
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.PostgreSQLDialect
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto= update
-
spring.datasource.username
&spring.datasource.password
properties are the same as your database installation. - Spring Boot uses Hibernate for JPA implementation, we configure
MySQL5InnoDBDialect
for MySQL orPostgreSQLDialect
for PostgreSQL -
spring.jpa.hibernate.ddl-auto
is used for database initialization. We set the value toupdate
value so that a table will be created in the database automatically corresponding to defined data model. Any change to the model will also trigger an update to the table. For production, this property should bevalidate
.
- For H2 database:
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto= update
spring.h2.console.enabled=true
# default path: h2-console
spring.h2.console.path=/h2-ui
-
spring.datasource.url
:jdbc:h2:mem:[database-name]
for In-memory database andjdbc:h2:file:[path/database-name]
for disk-based database. - We configure
H2Dialect
for H2 Database -
spring.h2.console.enabled=true
tells the Spring to start H2 Database administration tool and you can access this tool on the browser:http://localhost:8080/h2-console
. -
spring.h2.console.path=/h2-ui
is for H2 console's url, so the default urlhttp://localhost:8080/h2-console
will change tohttp://localhost:8080/h2-ui
.
Create Entity
In model package, we define Tutorial
class.
Tutorial has following fields: id, title, level, description, published, createdAt.
model/Tutorial.java
package com.bezkoder.spring.jpa.query.model;
import javax.persistence.*;
import java.util.Date;
@Entity
@Table(name = "tutorials")
public class Tutorial {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String title;
private String description;
private int level;
private boolean published;
@Temporal(TemporalType.TIMESTAMP)
private Date createdAt;
public Tutorial() {
}
public Tutorial(String title, String description, int level, boolean published, Date createdAt) {
this.title = title;
this.description = description;
this.level = level;
this.published = published;
this.createdAt = createdAt;
}
// getters and setters
}
-
@Entity
annotation indicates that the class is a persistent Java class. @Table
annotation provides the table that maps this entity.@Id
annotation is for the primary key.@GeneratedValue
annotation is used to define generation strategy for the primary key.@Temporal
annotation converts back and forth between timestamp andjava.util.Date
or time-stamp into time. For example,@Temporal(TemporalType.DATE)
drops the time value and only preserves the date.
@Temporal(TemporalType.DATE)
private Date createdAt;
JPA Repository Find by multiple Columns methods
Assume that we've already have tutorials table like this:
Let's create a repository to interact with database.
In repository package, create TutorialRepository
interface that extend JpaRepository
.
repository/TutorialRepository.java
package com.bezkoder.spring.jpa.query.repository;
import com.bezkoder.spring.jpa.query.model.Tutorial;
public interface TutorialRepository extends JpaRepository<Tutorial, Long> {
}
In this interface, we will write JPA Queries to filter data from database.
Findby multiple Fields using Derived Query
For multiple fields/multiple columns, we can use And
, Or
keywords between fields/columns.
Notice that you can concatenate as much And
/Or
as you want.
List<Tutorial> findByLevelAndPublished(int level, boolean isPublished);
List<Tutorial> findByTitleOrDescription(String title, String description);
Result:
tutorials = tutorialRepository.findByLevelAndPublished(3, true);
show(tutorials);
/*
Tutorial [id=1, title=Spring Data, description=Spring Data Description, level=3, published=true, createdAt=2022-03-11 00:00:00.0]
Tutorial [id=3, title=Hibernate, description=Hibernate ORM Description, level=3, published=true, createdAt=2022-04-26 00:00:00.0]
Tutorial [id=5, title=Spring JPA, description=Spring Data JPA Description, level=3, published=true, createdAt=2022-05-19 00:00:00.0]
*/
tutorials = tutorialRepository.findByTitleOrDescription("Hibernate", "Spring Data Description");
show(tutorials);
/*
Tutorial [id=1, title=Spring Data, description=Spring Data Description, level=3, published=true, createdAt=2022-03-11 00:00:00.0]
Tutorial [id=3, title=Hibernate, description=Hibernate ORM Description, level=3, published=true, createdAt=2022-04-26 00:00:00.0]
*/
We can perform SQL LIKE query with following keywords:
-
Like
: where x.field like param -
NotLike
: where x.field not like param -
StartingWith
: where x.field like %param (with appended%
) -
EndingWith
: where x.field like param% (with prepended%
) -
Containing
: where x.field like %param% (wrapped in %)
For case insensitive query, in SQL, we can force the value to all capital or lower case letters, then compare with the query values.
Spring JPA provide IgnoreCase
keyword to do this with Derived Query.
List<Tutorial> findByTitleContainingIgnoreCase(String title);
List<Tutorial> findByTitleContainingOrDescriptionContaining(String title, String description);
List<Tutorial> findByTitleContainingIgnoreCaseAndPublished(String title, boolean isPublished);
Result:
tutorials = tutorialRepository.findByTitleContainingIgnoreCase("dat");
show(tutorials);
/*
Tutorial [id=1, title=Spring Data, description=Spring Data Description, level=3, published=true, createdAt=2022-03-11 00:00:00.0]
*/
String text = "ot";
tutorials = tutorialRepository.findByTitleContainingOrDescriptionContaining(text, text);
show(tutorials);
/*
Tutorial [id=2, title=Java Spring Boot, description=Spring Framework Description, level=1, published=false, createdAt=2022-03-11 00:00:00.0]
Tutorial [id=4, title=Spring Boot, description=Spring Boot Description, level=2, published=false, createdAt=2022-04-26 00:00:00.0]
*/
tutorials = tutorialRepository.findByTitleContainingAndPublished("ring", true);
// or
// tutorials = tutorialRepository.findByTitleContainingIgnoreCaseAndPublished("spring", true);
show(tutorials);
/*
Tutorial [id=1, title=Spring Data, description=Spring Data Description, level=3, published=true, createdAt=2022-03-11 00:00:00.0]
Tutorial [id=5, title=Spring JPA, description=Spring Data JPA Description, level=3, published=true, createdAt=2022-05-19 00:00:00.0]
Tutorial [id=6, title=Spring Batch, description=Spring Batch Description, level=4, published=true, createdAt=2022-05-19 00:00:00.0]
*/
Findby multiple Fields using JPQL
Let's use @Query
annotation to create Spring JPA Query with SELECT and WHERE keywords.
@Query("SELECT t FROM Tutorial t WHERE LOWER(t.title) LIKE LOWER(CONCAT('%', :keyword,'%')) OR LOWER(t.description) LIKE LOWER(CONCAT('%', :keyword,'%'))")
List<Tutorial> findByTitleContainingOrDescriptionContainingCaseInsensitive(String keyword);
@Query("SELECT t FROM Tutorial t WHERE LOWER(t.title) LIKE LOWER(CONCAT('%', :title,'%')) AND t.published=:isPublished")
List<Tutorial> findByTitleContainingCaseInsensitiveAndPublished(String title, boolean isPublished);
Result:
tutorials = tutorialRepository.findByTitleContainingOrDescriptionContainingCaseInsensitive("data");
show(tutorials);
/*
Tutorial [id=1, title=Spring Data, description=Spring Data Description, level=3, published=true, createdAt=2022-03-11 00:00:00.0]
Tutorial [id=5, title=Spring JPA, description=Spring Data JPA Description, level=3, published=true, createdAt=2022-05-19 00:00:00.0]
*/
tutorials = tutorialRepository.findByTitleContainingCaseInsensitiveAndPublished("spring", true);
show(tutorials);
/*
Tutorial [id=1, title=Spring Data, description=Spring Data Description, level=3, published=true, createdAt=2022-03-11 00:00:00.0]
Tutorial [id=5, title=Spring JPA, description=Spring Data JPA Description, level=3, published=true, createdAt=2022-05-19 00:00:00.0]
Tutorial [id=6, title=Spring Batch, description=Spring Batch Description, level=4, published=true, createdAt=2022-05-19 00:00:00.0]
*/
Findby multiple Fields using Native Query
We also use @Query
annotation to create Spring JPA Native Query with SELECT and WHERE keywords.
@Query(value = "SELECT * FROM tutorials t WHERE LOWER(t.title) LIKE LOWER(CONCAT('%', :keyword,'%')) OR LOWER(t.description) LIKE LOWER(CONCAT('%', :keyword,'%'))", nativeQuery = true)
List<Tutorial> findByTitleContainingOrDescriptionContainingCaseInsensitive(String keyword);
@Query(value = "SELECT * FROM tutorials t WHERE LOWER(t.title) LIKE LOWER(CONCAT('%', :title,'%')) AND t.published=:isPublished", nativeQuery = true)
List<Tutorial> findByTitleContainingCaseInsensitiveAndPublished(String title, boolean isPublished);
Result:
tutorials = tutorialRepository.findByTitleContainingOrDescriptionContainingCaseInsensitive("data");
show(tutorials);
/*
Tutorial [id=1, title=Spring Data, description=Spring Data Description, level=3, published=true, createdAt=2022-03-11 00:00:00.0]
Tutorial [id=5, title=Spring JPA, description=Spring Data JPA Description, level=3, published=true, createdAt=2022-05-19 00:00:00.0]
*/
tutorials = tutorialRepository.findByTitleContainingCaseInsensitiveAndPublished("spring", true);
show(tutorials);
/*
Tutorial [id=1, title=Spring Data, description=Spring Data Description, level=3, published=true, createdAt=2022-03-11 00:00:00.0]
Tutorial [id=5, title=Spring JPA, description=Spring Data JPA Description, level=3, published=true, createdAt=2022-05-19 00:00:00.0]
Tutorial [id=6, title=Spring Batch, description=Spring Batch Description, level=4, published=true, createdAt=2022-05-19 00:00:00.0]
*/
Run JPA Filter by Multiple Columns project
Let's open SpringJpaRepositoryQueryExampleApplication.java
, we will implement CommandLineRunner
and autowire TutorialRepository
interface to run JPA Query methods here.
package com.bezkoder.spring.jpa.query;
// import ...
@SpringBootApplication
public class SpringJpaRepositoryQueryExampleApplication implements CommandLineRunner {
@Autowired
TutorialRepository tutorialRepository;
public static void main(String[] args) {
SpringApplication.run(SpringJpaRepositoryQueryExampleApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
// call tutorialRepository methods here
}
private void show(List<Tutorial> tutorials) {
tutorials.forEach(System.out::println);
}
}
Conclusion
Today we've known how to use JPA Repository to find by/filter by multiple columns in Spring Boot example using Derived Query, JPQL and Native Query.
We can query any number of fields separated by logical operators (AND, OR).
You can continue to write CRUD Rest APIs with:
Spring Boot, Spring Data JPA – Rest CRUD API example
If you want to write Unit Test for the JPA Repository:
Spring Boot Unit Test for JPA Repository with @DataJpaTest
You can also know:
- how to deploy this Spring Boot App on AWS (for free) with this tutorial.
- dockerize with Docker Compose: Spring Boot and MySQL example
- way to upload an Excel file and store the data in MySQL database with this post
- upload CSV file and store the data in MySQL with this post.
Happy learning! See you again.
Source Code
You can find the complete source code for this tutorial on Github that uses:
Further Reading
- Secure Spring Boot App with Spring Security & JWT Authentication
- Spring Data JPA Reference Documentation
- Spring Boot Pagination and Sorting example
Fullstack CRUD App:
- Vue + Spring Boot example
- Angular 8 + Spring Boot example
- Angular 10 + Spring Boot example
- Angular 11 + Spring Boot example
- Angular 12 + Spring Boot example
- Angular 13 + Spring Boot example
- Angular 14 + Spring Boot example
- React + Spring Boot example
Associations:
Top comments (0)