Annotates a repository method as a query method, specifying a query written in Jakarta Data Query Language (JDQL) or in Jakarta Persistence Query Language (JPQL). A Jakarta Data provider is not required to support the complete JPQL language, which targets relational data stores. However, a given provider might offer features of JPQL which go beyond the subset required by JDQL, or might even offer vendor-specific extensions to JDQL which target particular capabilities of the target data store technology. Such extensions come with no guarantee of portability between providers, nor between databases.
The required value member specifies the JDQL or JPQL query as a string.
For select statements, the return type of the query method must be consistent with the type returned by
the query. For update or delete statements, it must be void, int or long.
Compared to SQL, JDQL allows an abbreviated syntax for select statements:
from clause is optional in JDQL. When it is missing, the queried entity is determined by the return
type of the repository method, or, if the return type is not an entity type, by the primary entity type of the
repository.select clause is optional in both JDQL and JPQL. When it is missing, the query returns the queried
entity.A query might involve:
:name where the labels name are legal Java identifiers, or ?n where the labels n are sequential positive integers starting
from 1.A given query must not mix named and ordinal parameters.
Each parameter of an annotated query method must either:
@Param)
and type as a named parameter of the query,Limit, Order, PageRequest, or Sort.The Param annotation associates a method parameter with a named parameter. The Param annotation is
unnecessary when the method parameter name matches the name of a named parameter and the application is compiled with
the -parameters compiler option making parameter names available at runtime.
A method parameter is associated with an ordinal parameter by its position in the method parameter list. The first
parameter of the method is associated with the ordinal parameter ?1.
For example,
@Repository
public interface People extends CrudRepository<Person, Long> {
// JDQL with positional parameters
@Query("where firstName = ?1 and lastName = ?2")
List<Person> byName(String first, String last);
// JDQL with a named parameter
@Query("where firstName || ' ' || lastName like :pattern")
List<Person> byName(String pattern);
// JPQL using a positional parameter
@Query("from Person where extract(year from birthdate) = ?1")
List<Person> bornIn(int year);
// JPQL using named parameters
@Query("select distinct name from Person " +
"where length(name) >= :min and length(name) <= :max")
Page<String> namesOfLength(@Param("min") int minLength,
@Param("max") int maxLength,
PageRequest pageRequest,
Order<Person> order);
...
}
A method annotated with @Query must return one of the following types:
R, when the query returns a single result,Optional<R>, when the query returns at most a single result,R[],
List<R>,Stream<R>, orPage<R> or CursoredPage<R>.The method returns an object for every query result.
R or Optional<R> and more than one record satisfies
the query restriction, the method must throw jakarta.data.exceptions.NonUniqueResultException .R and no record satisfies the query restriction, the method
must throw jakarta.data.exceptions.EmptyResultException .Annotations such as @Find, @Query, @Insert, @Update, @Delete, and
@Save are mutually-exclusive. A given method of a repository interface may have at most one @Find
annotation, lifecycle annotation, or query annotation.
Param
| Modifier and Type | Method and Description |
|---|---|
| public String | Returns: the query to be executed when the annotated method is called.Specifies the query executed by the annotated repository method, in JDQL or JPQL. |
| value | back to summary |
|---|---|
| public String value() Specifies the query executed by the annotated repository method, in JDQL or JPQL. If the annotated repository method accepts other forms of sorting
(such as a parameter of type
| |