Interface TypedQueryReference<R>
- Type Parameters:
R- an upper bound on the result type of the query
- All Superinterfaces:
Reference
- All Known Implementing Classes:
StaticTypedQueryReference
NamedQuery or NamedNativeQuery annotations,
or using JakartaQuery or
NativeQuery. An
instance of TypedQueryReference is usually obtained
from the static metamodel of the annotated type.
In this example, a method is annotated, and the name of
the query is determined by the name of the annotated method.
The annotated query is executed with the given argument to
the query parameter named pattern and with the
CacheStoreMode.BYPASS option:
class Library {
@Inject EntityManager entityManager;
@JakartaQuery("select a from Book b join b.authors a where b.title like :pattern")
@QueryOptions(cacheStoreMode = CacheStoreMode.BYPASS)
List<Author> findAuthorsGivenTitles(String pattern) {
return entityManager.createQuery(Library_.findAuthorsGivenTitles(pattern))
.getResultList();
}
}
In this example, it is the entity class that is annotated,
and the NamedQuery annotation explicitly specifies a
name:
@NamedQuery(name = "byTitle",
query = "from Book where title like ?1")
@Entity class Book { .. }
In this case the TypedQueryReference obtained from
its static metamodel does not include arguments to the query
parameters, and so they must be supplied via setParameter:
var books =
entityManager.createQuery(Book_._byTitle_)
.setCacheStoreMode(CacheStoreMode.BYPASS)
.setParameter(1, pattern)
.getResultList();
A TypedQueryReference may include arguments to
parameters of the query.
- A reference representing a query declared using an
annotation of a type or field has no arguments, and so
Reference.getArguments(),Reference.getParameterNames(), andReference.getParameterTypes()all returnnull. - A reference representing a query declared using an
annotation of a method holds information about the
arguments passed to the method, making it available via
Reference.getArguments(),Reference.getParameterNames(), andReference.getParameterTypes().
In the Jakarta Persistence query language, only SELECT
queries are typed queries, since only a SELECT query can
return a result. A DELETE or UPDATE statement is not a
typed query and is always represented by an untyped
instance of StatementReference. On the other hand, a
native SQL query is considered a typed query if it returns
a result set.
- Since:
- 3.2
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionThe specified entity graph name, if any, ornullif no entity graph was specified.All options specified byQueryOptions,NamedQuery, orNamedNativeQuery, or an empty set if no options were specified.The result type of the query, as specified byNamedQuery.resultClass()orNamedNativeQuery.resultClass(), or as inferred from the declared return type of the method annotatedJakartaQueryorNativeQuery.Methods inherited from interface Reference
getArguments, getHints, getName, getParameterNames, getParameterTypes
-
Method Details
-
getResultType
The result type of the query, as specified byNamedQuery.resultClass()orNamedNativeQuery.resultClass(), or as inferred from the declared return type of the method annotatedJakartaQueryorNativeQuery. -
getOptions
All options specified byQueryOptions,NamedQuery, orNamedNativeQuery, or an empty set if no options were specified.- Since:
- 4.0
- See Also:
-
getEntityGraphName
The specified entity graph name, if any, ornullif no entity graph was specified.- Since:
- 4.0
- See Also:
-