Top Description Methods
jakarta.data.repository

public @Interface Query

extends Annotation
Annotations
@Retention:RUNTIME
@Target:METHOD
Imports
jakarta.data.Sort, java.lang.annotation.ElementType, .Retention, .RetentionPolicy, .Target

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:

A query might involve:

A given query must not mix named and ordinal parameters.

Each parameter of an annotated query method must either:

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:

The method returns an object for every query result.

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.

See Also
Param

Method Summary

Modifier and TypeMethod and Description
public String

Returns:

the query to be executed when the annotated method is called.
value
()

Specifies the query executed by the annotated repository method, in JDQL or JPQL.

Inherited from java.lang.annotation.Annotation:
annotationTypeequalshashCodetoString

Method Detail

valueback 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 Sort), it is the responsibility of the application programmer to compose the query so that an ORDER BY clause can be validly appended to the text of the query.

Returns:String

the query to be executed when the annotated method is called.