Interface TypedQuery<T>
- Type Parameters:
-
T- the type of the result objects
- All Superinterfaces:
-
Query
Query
that maps query results directly to the target type
T. This type can be either:
- An entity class annotated with
@Entity -
A projection class annotated with
Projection
One of the key benefits of using TypedQuery is that
the FROM clause can be omitted when the provider is
able to infer the source entity from the T type:
-
If
Tis an entity, theFROMclause is optional. -
If
Tis a projection with the@Projection(from = ...)annotation, the entity source is inferred from it.
This simplifies queries significantly and avoids boilerplate.
Important: When using a
FROM clause explicitly in the query string, the
entity specified in the query must match the class provided as
T. For example, the following is
invalid and must raise an error:
// Assuming Cat and Dog are both entities
template.typedQuery("FROM Cat", Dog.class); //Must fail
This validation ensures consistency between the declared return type and the query structure.
Examples:
Using entity type with inferred FROM clause:
List<Product> products = template
.typedQuery("WHERE category = 'TECH'", Product.class)
.result();
Using projection type with inferred FROM clause:
@Projection(from = Product.class)
public record PromotionalProduct(String name, double price) {}
List<PromotionalProduct> promos = template
.typedQuery("WHERE price < 100", PromotionalProduct.class)
.result();
- Since:
- 1.1.0
-
Method Summary
Methods inherited from interface Query
executeUpdate
-
Method Details
-
result
Executes theSELECTquery and returns the results as aListof typeT.This method can only be used with
SELECTqueries. It throws anUnsupportedOperationExceptionif called for a non-select query such asUPDATEorDELETE.List<TechProductView> results = template .typedQuery("FROM Product WHERE category = 'TECH'", TechProductView.class) .result();- Specified by:
-
resultin interfaceQuery - Returns:
- list of results or an empty list
- Throws:
-
UnsupportedOperationException- if the query is not aSELECT
-
stream
Executes theSELECTquery and returns the results as aStreamof typeT.This is useful for processing large result sets in a streaming fashion. It throws an
UnsupportedOperationExceptionif the query is not aSELECT.Stream<TechProductView> stream = template .typedQuery("FROM Product WHERE active = true", TechProductView.class) .stream(); stream.forEach(System.out::println);}- Specified by:
-
streamin interfaceQuery - Returns:
- stream of results
- Throws:
-
UnsupportedOperationException- if the query is not aSELECT
-
singleResult
Executes theSELECTquery and returns a single result wrapped in anOptional.If no result is found, returns
Optional.empty(). If more than one result is found, the behavior is provider-specific and may result in an exception.Optional<Product> product = template .typedQuery("WHERE id = :id", Product.class) .bind("id", "p-42") .singleResult();- Specified by:
-
singleResultin interfaceQuery - Returns:
- optional containing the result or empty
- Throws:
-
UnsupportedOperationException- if the query is not aSELECT
-
bind
Binds a named parameter to the query.TypedQuery<Product> query = template.typedQuery("SELECT * FROM Book WHERE title = :title", Product.class) .bind("title", "Effective Java");- Specified by:
-
bindin interfaceQuery - Parameters:
-
name- the parameter name (without:) value- the value to bind- Returns:
- this query instance for fluent chaining
- Throws:
-
NullPointerException- if the name is null
-
bind
Binds a positional parameter to the query. Positions are 1-based.TypedQuery<Product> query = template.query("SELECT * FROM Person WHERE age > ?1", Product.class) .bind(1, 30);- Specified by:
-
bindin interfaceQuery - Parameters:
-
position- the parameter position (starting at 1) value- the value to bind- Returns:
- this query instance for fluent chaining
- Throws:
-
IllegalArgumentException- if position is less than 1
-