Interface StatementOrTypedQuery
- All Superinterfaces:
Query
Declares operations allowing a
Statement or TypedQuery
to be obtained when the type of a query or statement has not yet been
specified. This allows a slightly more fluent programming style where:
- a Jakarta Persistence or native SQL query is provided to
createQuery(), and - the type of the query clarified in a subsequent method call.
If an instance of this interface represents an UPDATE
or DELETE statement, then a Statement representing
the same statement may be obtained by calling asStatement().
int updated =
em.createQuery("delete from Temporary where timestamp > ?1")
.asStatement()
.setParameter(1, cutoffDateTime)
.execute();
If an instance of this interface represents a SELECT
query, then a TypedQuery representing the same query may
be obtained by calling ofType(Class), passing the result
type of the query.
List<Book> books =
em.createQuery("from Book where extract(year from publicationDate) > :year")
.ofType(Book.class)
.setParameter("year", Year.of(2000))
.setMaxResults(10)
.setCacheRetrieveMode(CacheRetrieveMode.BYPASS)
.getResultList();
These operations may be viewed as a sort of type cast to a given subtype of this interface.
- Since:
- 4.0
- API note:
- For backward compatibility with older versions of the
Persistence API, it is possible to execute a query or statement
by calling the deprecated methods
Query.getResultList()andQuery.executeUpdate()inherited by this interface. But new code targeting Persistence 4 should not use this interface for direct execution of statements or queries. Statements should be executed using theStatementinterface, and queries should be executed using theTypedQueryinterface.
-
Method Summary
Modifier and TypeMethodDescriptionObtain aStatementrepresenting this statement, which must be some kind of executable statement, that is, a Jakarta PersistenceUPDATEorDELETEstatement, or any native SQL statement that returns a row count.<R> TypedQuery<R> Obtain aTypedQuerywith the given query result type, which must be a supertype of the result type of this query.<R> TypedQuery<R> withEntityGraph(EntityGraph<R> graph) Obtain aTypedQuerywith the given entity graph, which must be rooted at a supertype of the result type of this query.<R> TypedQuery<R> withResultSetMapping(ResultSetMapping<R> mapping) Obtain aTypedQuerywith the given result set mapping.Methods inherited from interface Query
executeUpdate, getCacheRetrieveMode, getCacheStoreMode, getFirstResult, getFlushMode, getHints, getLockMode, getMaxResults, getParameter, getParameter, getParameter, getParameter, getParameters, getParameterValue, getParameterValue, getParameterValue, getQueryFlushMode, getResultList, getResultStream, getSingleResult, getSingleResultOrNull, getTimeout, isBound, setCacheRetrieveMode, setCacheStoreMode, setConvertedParameter, setConvertedParameter, setFirstResult, setFlushMode, setHint, setLockMode, setMaxResults, setParameter, setParameter, setParameter, setParameter, setParameter, setParameter, setParameter, setParameter, setParameter, setParameter, setParameter, setParameter, setParameter, setQueryFlushMode, setTimeout, setTimeout, unwrap
-
Method Details
-
asStatement
Obtain aStatementrepresenting this statement, which must be some kind of executable statement, that is, a Jakarta PersistenceUPDATEorDELETEstatement, or any native SQL statement that returns a row count. The executable statement may be executed by callingStatement.execute().- Throws:
IllegalStateException- if this query is a Jakarta PersistenceSELECTquery
-
ofType
Obtain aTypedQuerywith the given query result type, which must be a supertype of the result type of this query. This query must be a Jakarta PersistenceSELECTquery or a native SQL query which returns a result set.- Type Parameters:
R- The query result type- Parameters:
resultType- The Java class of the query result type- Throws:
IllegalArgumentException- if the given result type is not a supertype of the result type of this queryIllegalStateException- if this query is a Jakarta PersistenceUPDATEorDELETEstatement
-
withEntityGraph
Obtain aTypedQuerywith the given entity graph, which must be rooted at a supertype of the result type of this query. This query must be a Jakarta PersistenceSELECTquery which returns a single entity type.- Type Parameters:
R- The query result type- Parameters:
graph- The entity graph, interpreted as a load graph- Throws:
IllegalArgumentException- if the given graph type is not rooted at a supertype of the result type of this queryIllegalStateException- if this query is a Jakarta PersistenceUPDATEorDELETEstatement
-
withResultSetMapping
Obtain aTypedQuerywith the given result set mapping. This query must be a native SQL query returning a result set compatible with the given mapping.- Type Parameters:
R- The query result type- Parameters:
mapping- The result set mapping- Throws:
IllegalArgumentException- if the given graph type is not rooted at a supertype of the result type of this queryIllegalStateException- if this query is a Jakarta Persistence query or statement
-