Spring Data JPA Query Method - Multiple Fields

In this tutorial, we will learn how to write a query method with multiple columns/fields in the Spring Data JPA repository.

YouTube Video

Spring Data JPA query methods are the most powerful methods, we can create query methods to select the records from the database without writing SQL queries. Behind the scenes, Spring Data JPA will create SQL queries based on the query method and execute the query for us.
Let's follow the Spring Data JPA naming convention to write a query method with multiple fields for the Product entity class.

We need to create a method starting with the prefix findBy followed by the field name. If we want to create query methods for multiple field names then we need to use logical operators such as AND, OR between the field names - findBy[FieldName1][AND/OR][FieldName2]

Consider the following Product entity class and if we want to retrieve products by their name OR description fields then here is the Spring Data JPA query method:

public List<Product> findByNameOrDescription(String name, String description);
Let's create a complete example to understand end-to-end.

Maven Dependencies

Add the following maven dependencies to your Spring Boot project:


Product Entity

Let's first create a Product entity class and add the following content to it:
package net.javaguides.springdatajpacourse.entity;

import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import jakarta.persistence.*;
import java.math.BigDecimal;
import java.util.Date;

public class Product {

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "sku")
    private String sku;

    @Column(name = "name")
    private String name;

    @Column(name = "description")
    private String description;

    @Column(name = "price")
    private BigDecimal price;

    @Column(name = "image_url")
    private String imageUrl;

    @Column(name = "active")
    private boolean active;

    @Column(name = "date_created")
    private Date dateCreated;

    @Column(name = "last_updated")
    private Date lastUpdated;

    public Long getId() {
        return id;

    public void setId(Long id) {
        this.id = id;

    public String getSku() {
        return sku;

    public void setSku(String sku) {
        this.sku = sku;

    public String getName() {
        return name;

    public void setName(String name) {
        this.name = name;

    public String getDescription() {
        return description;

    public void setDescription(String description) {
        this.description = description;

    public BigDecimal getPrice() {
        return price;

    public void setPrice(BigDecimal price) {
        this.price = price;

    public String getImageUrl() {
        return imageUrl;

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;

    public boolean isActive() {
        return active;

    public void setActive(boolean active) {
        this.active = active;

    public Date getDateCreated() {
        return dateCreated;

    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;

    public Date getLastUpdated() {
        return lastUpdated;

    public void setLastUpdated(Date lastUpdated) {
        this.lastUpdated = lastUpdated;

    public String toString() {
        return "Product{" +
                "id=" + id +
                ", sku='" + sku + '\'' +
                ", name='" + name + '\'' +
                ", description='" + description + '\'' +
                ", price=" + price +
                ", imageUrl='" + imageUrl + '\'' +
                ", active=" + active +
                ", dateCreated=" + dateCreated +
                ", lastUpdated=" + lastUpdated +


Let's create ProductRepository which extends JpaRepository and add the following code to it:
import java.util.List;

import net.javaguides.springdatajpacourse.entity.Product;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ProductRepository extends JpaRepository<Product, Long> {

	 * Returns the found product entry whose title or description is given
	 * as a method parameter. If no product entry is found, this method
	 * returns an empty list.
	public List<Product> findByNameOrDescription(String name, String description);
Note that the below query method returns the found product entry whose title or description is given as a method parameter. If no product entry is found, this method returns an empty list.
public List<Product> findByNameOrDescription(String name, String description);

Configure MySQL and Hibernate Properties

Let's use the MySQL database to store and retrieve the data in this example and we gonna use Hibernate properties to create and drop tables.

Open the application.properties file and add the following configuration to it:


spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect

spring.jpa.hibernate.ddl-auto = create-drop

Make sure that you will create ecommerce database before running the Spring boot application.
Also, change the MySQL username and password as per your MySQL installation on your machine.

Testing Query Method with Multiple Fields

In order to test the query method with multiple fields that we have created, we gonna use CommandLineRunner.run() method to execute the testing code while the Spring boot application startup: 
import net.javaguides.springdatajpacourse.entity.Product;
import net.javaguides.springdatajpacourse.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public class SpringDataJpaCourseApplication implements CommandLineRunner{

	public static void main(String[] args) throws Exception {
		SpringApplication.run(SpringDataJpaCourseApplication.class, args);

	private ProductRepository productRepository;

	public void run(String... args) throws Exception {
        Product product = new Product();
        product.setName("product 1");
        product.setDescription("product 1 desc");
        product.setPrice(new BigDecimal(100));
        product.setDateCreated(new Date());
        product.setLastUpdated(new Date());
        product.setSku("product 1 sku");

        // save product

        Product product2 = new Product();
        product2.setName("product 2");
        product2.setDescription("product 2 desc");
        product2.setPrice(new BigDecimal(200));
        product2.setDateCreated(new Date());
        product2.setLastUpdated(new Date());
        product2.setSku("product 2 sku");

        // save product 2

        // test query method with multiple parameters
        List<Product> products = productRepository.findByNameOrDescription("product 1"
        ,"product 1 desc");
        products.forEach((p) ->
Once Spring boot application execution completes, you can able to see Spring Data JPA (uses Hibernate as JPA provider) generated SQL statements in a console:
        (active, date_created, description, image_url, last_updated, name, price, sku) 
        (?, ?, ?, ?, ?, ?, ?, ?)
        (active, date_created, description, image_url, last_updated, name, price, sku) 
        (?, ?, ?, ?, ?, ?, ?, ?)
        product0_.id as id1_0_,
        product0_.active as active2_0_,
        product0_.date_created as date_cre3_0_,
        product0_.description as descript4_0_,
        product0_.image_url as image_ur5_0_,
        product0_.last_updated as last_upd6_0_,
        product0_.name as name7_0_,
        product0_.price as price8_0_,
        product0_.sku as sku9_0_ 
        products product0_ 
        or product0_.description=?

Related Spring Data JPA Query Method Tutorials