Query Methods or Finder Methods

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Creating Database Queries

From Method Names


By Ramesh Fadatare (Java Guides)
Query generation from method names
Spring Data JPA query methods are the most powerful methods, we can create
query methods to select 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.
ndByName(String name)

select id, name, description, active, image_url, price, sku from products where name=“product name”

We can create query methods for repository using Entity elds

Creating query methods is also called nder methods ( ndBy, ndAll …)


fi
fi
fi
fi
fi
Query creation/generation from
method names works
We write Query Method using Spring Data JPA

Parsing query method names is divided into subject


Spring Data JPA parse Query method and creates and predicate. The rst part ( nd…By, exists…By)
JPA Criteria de nes the subject of the query, the second part
forms the predicate.

JPA criteria creates JPQL query and executes it


fi
fi
fi
Query Method Structure
Query method
return type Query method
Predicate keyword

Method parameters
Query method
Subject keyword

Rules to create query methods


1. The name of our query method must start with one of the following pre xes: nd…By,
read…By, query…By, count…By, and get…By
Examples: ndByName, readByName, queryByName, getByName

2. If we want to limit the number of returned query results, we can add the First or the Top
keyword before the rst By word
Examples: ndFirstByName, readFirst2ByName, ndTop10ByName

3. If we want to select unique results, we have to add the Distinct keyword before the rst
By word.
Examples: ndDistinctByName or ndNameDistinctBy

4. Combine property expression with AND and OR.


Examples: ndByNameOrDescription, ndByNameAndDescriptio

fi
fi
fi
fi
fi
.

fi
fi
.

fi

fi
fi
fi
Returning Values From Query Methods
A query method can return only one result or more than one result.

1. if we are writing a query that should return only one result, we can return the following types

• Basic type. Our query method will return the found basic type or null
• Entity. Our query method will return an entity object or null
• Guava / Java 8 Optional<T>. Our query method will return an Optional that contains the found
object or an empty Optional.

2. if we are writing a query method that should return more than one result, we can return the
following types

• List<T>. Our query method will return a list that contains the query results or an empty list
• Stream<T>. Our query method will return a Stream that can be used to access the query results or
an empty Stream.
:

Query or nder methods examples

fi
Query method - Find by single eld name
Write a query method to nd or retrieve a product by name

Write a query method to nd or retrieve a product by id


fi
fi
fi
Query method - Find by multiple
eld names
Write a query method to nd or retrieve a product by name or description

Write a query method to nd or retrieve a product by name and description


fi
fi
fi

Query method - Find by Distinct


Write a query method to nd or retrieve a unique product by name

fi
Query method - Find by GreaterThan
Write a query method to nd or retrieve products whose price is greater
than given price as method parameter

fi
Query method - Find by LessThan
Write a query method to nd or retrieve products whose price is less
than given price as method parameter

fi
Query method - Find by Containing
Write a query method to nd or retrieve ltered products that match
the given text ( contains check)

fi
fi
Query method - Find by Like
Write a query method to nd or retrieve products for a speci ed pattern
in a column ( SQL LIKE condition)

fi
fi
Query method - Between
Write a query method to nd or retrieve products based on the price
range ( start price and end price)

fi
Query method - Between
Write a query method to nd or retrieve products based on the start
date and end date

fi
Query method - In
Write a query method to nd or retrieve products based on multiple
values ( specify multiple values in a SQL where clause)

fi
Query method - Limiting Query Result
Spring Data JPA supports keywords ' rst' or 'top' to limit the query
results
Example: ndFirstByName(), ndTop5BySku(

An optional numeric value can be appended after 'top' or ' rst' to limit
the maximum number of results to be returned (e.g. ndTop3By....). If
this number is not used then only one entity is returned.

There's no difference between the keywords ' rst' and 'top'.


.

fi
fi
fi
fi
)

fi

fi
s

When Should We Use Query Methods


This query generation strategy has the following bene ts:
• Creating simple queries is fast
• The method name of our query method describes the selected value(s) and the
used search condition(s).

This query generation strategy has the following weaknesses:


• The features of the method name parser determine what kind of queries we can
create. If the method name parser doesn’t support the required keyword, we
cannot use this strategy
• The method names of complex query methods are long and ugly.
• There is no support for dynamic queries.
.

fi

You might also like