Interface TypedQueryReference<R>

Type Parameters:
R - an upper bound on the result type of the query
All Superinterfaces:
Reference
All Known Implementing Classes:
StaticTypedQueryReference

public non-sealed interface TypedQueryReference<R> extends Reference
A reference to a typed named query declared via the 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.

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: