Interface QueryProducer
- All Known Subinterfaces:
EventSource,QueryProducerImplementor,Session,SessionImplementor,SharedSessionContract,SharedSessionContractImplementor,StatelessSession
- All Known Implementing Classes:
AbstractDelegateSessionImplementor,AbstractSharedSessionContract,SessionDelegatorBaseImpl,SessionImpl,SessionLazyDelegator,SharedSessionDelegatorBaseImpl,StatelessSessionImpl,ToOneDelegateSessionImplementor
SelectionQuery and MutationQuery.
Implementors include Session and StatelessSession.
Many operations of the interface have the same or very similar signatures to operations of
EntityManager. They are declared here to allow reuse by
StatelessSession.
There are three fundamental ways to express a query:
- in Hibernate Query Language, an object-oriented query dialect of SQL which is a superset of the Jakarta Persistence Query Language,
- in the native SQL dialect of the database, or
- using the Criteria API defined by JPA, along with extensions defined by Hibernate.
In each case, the object used to execute the query depends on whether the query is a selection query or a mutation query.
- selection queries are executed via an instance of
SelectionQuery, while - mutation queries are executed via an instance of
MutationQuery, but - since JPA makes no such distinction within its API, the type
Queryis a mixin ofSelectionQuery,MutationQuery, andTypedQuery.
Operations like createSelectionQuery(),
createNamedSelectionQuery(), and
createNativeQuery() accept a Java
class object indicating the result type of the query.
- The result type might be an entity class, when
the query returns an entity:
List<Book> allBooks = session.createNativeQuery("select * from books order by title", Book.class) .getResultList(); - It might be a basic type like
StringorLong:List<String> allTitles = session.createNativeQuery("select distinct title from books order by title", String.class) .getResultList(); - Finally, the result type might be a class used to package the elements of a
selectlist, such as a Java record with an appropriate constructor,Map,List, orObject[]:record IsbnAndTitle(String isbn, String title) {} List<IsbnAndTitle> allBooks = session.createNativeQuery("select isbn, title from books order by title", IsbnAndTitle.class) .getResultList();
AbstractQuery.getResultType().- API Note:
- Unlike the corresponding operations of
EntityManager, operations for creating untyped instances ofQueryare all marked as deprecated. Clients must migrate to the use of the equivalent operations which accept aClassand return a typedQuery.
-
Method Summary
Modifier and TypeMethodDescriptioncreateMutationQuery(CriteriaDelete deleteQuery) Create aMutationQueryfrom the given delete criteria treecreateMutationQuery(CriteriaUpdate updateQuery) Create aMutationQueryfrom the given update criteria treecreateMutationQuery(String hqlString) Create aMutationQueryreference for the given HQL insert, update, or delete statement.createMutationQuery(JpaCriteriaInsert insert) Create aMutationQueryfrom the given insert criteria treeCreate aMutationQueryinstance for the given named insert, update, or delete HQL query.createNamedQuery(String name) Deprecated.<R> Query<R> createNamedQuery(String name, Class<R> resultClass) Create a typedQueryinstance for the given named query.Deprecated.<R> SelectionQuery<R> createNamedSelectionQuery(String name, Class<R> resultType) Create aSelectionQueryinstance for the namedNamedQuerywith the given result type.createNativeMutationQuery(String sqlString) Create aNativeQueryinstance for the given native SQL statement.createNativeQuery(String sqlString) Deprecated.<R> NativeQuery<R> createNativeQuery(String sqlString, Class<R> resultClass) Create aNativeQueryinstance for the given native SQL query using an implicit mapping to the specified Java type.<R> NativeQuery<R> createNativeQuery(String sqlString, Class<R> resultClass, String tableAlias) Create aNativeQueryinstance for the given native SQL query using an implicit mapping to the specified Java entity type.createNativeQuery(String sqlString, String resultSetMappingName) Deprecated.<R> NativeQuery<R> createNativeQuery(String sqlString, String resultSetMappingName, Class<R> resultClass) Create aNativeQueryinstance for the given native SQL query using an explicit mapping to the specified Java type.createQuery(CriteriaDelete deleteQuery) Deprecated.<R> Query<R> createQuery(CriteriaQuery<R> criteriaQuery) Create aQueryfor the given JPACriteriaQuery.createQuery(CriteriaUpdate updateQuery) Deprecated.<R> Query<R> createQuery(TypedQueryReference<R> typedQueryReference) Create a typedQueryinstance for the given typed query reference.createQuery(String queryString) Deprecated.The overloaded formcreateQuery(String, Class)which takes a result type is strongly recommended in preference to this method, since it returns a typedQueryobject, and because it is able to use the given result type to infer theselectlist, and even sometimes thefromclause.<R> Query<R> createQuery(String queryString, Class<R> resultClass) Create a typedQueryinstance for the given HQL query string and given query result type.<R> SelectionQuery<R> createSelectionQuery(CriteriaQuery<R> criteria) Create aSelectionQueryreference for the givenCriteriaQuery.createSelectionQuery(String hqlString) Deprecated.The overloaded formcreateSelectionQuery(String, Class)which takes a result type is strongly recommended in preference to this method, since it returns a typedSelectionQueryobject, and because it is able to use the given result type to infer theselectlist, and even sometimes thefromclause.<R> SelectionQuery<R> createSelectionQuery(String hqlString, EntityGraph<R> resultGraph) Create aSelectionQueryinstance for the given HQL query string and givenEntityGraph, which is interpreted as a load graph.<R> SelectionQuery<R> createSelectionQuery(String hqlString, Class<R> resultType) Create aSelectionQueryinstance for the given HQL query string and given query result type.getNamedNativeQuery(String name) Deprecated.getNamedNativeQuery(String name, String resultSetMapping) Deprecated.getNamedQuery(String queryName) Deprecated.
-
Method Details
-
createQuery
Deprecated.The overloaded formcreateQuery(String, Class)which takes a result type is strongly recommended in preference to this method, since it returns a typedQueryobject, and because it is able to use the given result type to infer theselectlist, and even sometimes thefromclause. Alternatively,createSelectionQuery(String, Class)is preferred for queries, andcreateMutationQuery(String)for insert, update, and delete statements.Create aQueryinstance for the given HQL query, or HQL insert, update, or delete statement.If a query has no explicit
selectlist, the select list is inferred:- if there is exactly one root entity in the
fromclause, and it has no non-fetchjoins, then that root entity is the only element of the select list, or - if there is an entity with the alias
this, then that entity is the only element of the select list, or - otherwise, the query is considered ambiguous, and this
method throws a
SemanticException.
The query must have an explicit
fromclause, which can never be inferred.- Parameters:
queryString- The HQL query- Returns:
- The
Queryinstance for manipulation and execution - See Also:
- API Note:
- Returns a raw
Querytype instead of a wildcard typeQuery<?>, to match the signature of the JPA methodEntityManager.createQuery(String).
- if there is exactly one root entity in the
-
createQuery
Create a typedQueryinstance for the given HQL query string and given query result type.- If the query has a single item in the
selectlist, then the select item must be assignable to the given result type. - Otherwise, if there are multiple select items, then the
select items will be packaged into an instance of the
result type. The result type must have an appropriate
constructor with parameter types matching the select items,
or it must be one of the types
Object[],List,Map, orTuple.
If a query has no explicit
selectlist, the select list is inferred from the given query result type:- if the result type is an entity type, the query must have
exactly one root entity in the
fromclause, it must be assignable to the result type, and the inferred select list will contain just that entity, or - otherwise, the select list contains every root entity and
every non-
fetchjoined entity, and each query result will be packaged into an instance of the result type, just as specified above.
If a query has no explicit
fromclause, and the given result type is an entity type, the root entity is inferred to be the result type.Passing
Object.classas the query result type is not recommended. In this special case, this method has the same semantics as the overloadcreateQuery(String).The returned
Querymay be executed by callingQuery.getResultList()orQuery.getSingleResult(). - If the query has a single item in the
-
createQuery
Create a typedQueryinstance for the given typed query reference.- Parameters:
typedQueryReference- the type query reference- Returns:
- The
Queryinstance for execution - Throws:
IllegalArgumentException- if a query has not been defined with the name of the typed query reference or if the query result is found to not be assignable to result class of the typed query reference- See Also:
-
createQuery
Create aQueryfor the given JPACriteriaQuery. -
createQuery
Deprecated.Create aMutationQueryfor the given JPACriteriaUpdate -
createQuery
Deprecated.Create aMutationQueryfor the given JPACriteriaDelete -
createNativeQuery
Deprecated.Create aNativeQueryinstance for the given native SQL query.- Parameters:
sqlString- a native SQL query string- Returns:
- The
NativeQueryinstance for manipulation and execution - See Also:
-
createNativeQuery
Create aNativeQueryinstance for the given native SQL query using an implicit mapping to the specified Java type.- If the given class is an entity class, this method is equivalent
to
createNativeQuery(sqlString).addEntity(resultClass). - If the given class has a registered
JavaType, then the query must return a result set with a single column whoseJdbcTypeis compatible with thatJavaType. - Otherwise, the select items will be packaged into an instance of
the result type. The result type must have an appropriate
constructor with parameter types matching the select items, or it
must be one of the types
Object[],List,Map, orTuple.
- Parameters:
sqlString- The native (SQL) query stringresultClass- The Java type to map results to- Returns:
- The
NativeQueryinstance for manipulation and execution - See Also:
- If the given class is an entity class, this method is equivalent
to
-
createNativeQuery
Create aNativeQueryinstance for the given native SQL query using an implicit mapping to the specified Java entity type.The given class must be an entity class. This method is equivalent to
createNativeQuery(sqlString).addEntity(tableAlias, resultClass).- Parameters:
sqlString- Native (SQL) query stringresultClass- The Java entity class to map results totableAlias- The table alias for columns in the result set- Returns:
- The
NativeQueryinstance for manipulation and execution - See Also:
-
createNativeQuery
@Deprecated(since="6.0") NativeQuery createNativeQuery(String sqlString, String resultSetMappingName) Deprecated.Create aNativeQueryinstance for the given native SQL query using an explicit mapping to the specified Java type.The given result set mapping name must identify a mapping defined by a
SqlResultSetMappingannotation.- Parameters:
sqlString- The native (SQL) query stringresultSetMappingName- The explicit result mapping name- Returns:
- The
NativeQueryinstance for manipulation and execution - See Also:
-
createNativeQuery
<R> NativeQuery<R> createNativeQuery(String sqlString, String resultSetMappingName, Class<R> resultClass) Create aNativeQueryinstance for the given native SQL query using an explicit mapping to the specified Java type.The given result set mapping name must identify a mapping defined by a
SqlResultSetMappingannotation.- Parameters:
sqlString- The native (SQL) query stringresultSetMappingName- The explicit result mapping name- Returns:
- The
NativeQueryinstance for manipulation and execution - See Also:
-
createSelectionQuery
Deprecated.The overloaded formcreateSelectionQuery(String, Class)which takes a result type is strongly recommended in preference to this method, since it returns a typedSelectionQueryobject, and because it is able to use the given result type to infer theselectlist, and even sometimes thefromclause.Create aSelectionQueryreference for the given HQLselectstatement.If the statement has no explicit
selectlist, the select list is inferred:- if there is exactly one root entity in the
fromclause, and it has no non-fetchjoins, then that root entity is the only element of the select list, or - if there is an entity with the alias
this, then that entity is the only element of the select list, or - otherwise, the query is considered ambiguous, and this
method throws a
SemanticException.
The query must have an explicit
fromclause, which can never be inferred.- Throws:
IllegalSelectQueryException- if the given HQL query is aninsert,updateordeletestatement
- if there is exactly one root entity in the
-
createSelectionQuery
Create aSelectionQueryinstance for the given HQL query string and given query result type.- If the query has a single item in the
selectlist, then the select item must be assignable to the given result type. - Otherwise, if there are multiple select items, then the
select items will be packaged into an instance of the
result type. The result type must have an appropriate
constructor with parameter types matching the select items,
or it must be one of the types
Object[],List,Map, orTuple.
If a query has no explicit
selectlist, the select list is inferred from the given query result type:- if the result type is an entity type, the query must have
exactly one root entity in the
fromclause, it must be assignable to the result type, and the inferred select list will contain just that entity, or - otherwise, the select list contains every root entity and
every non-
fetchjoined entity, and each query result will be packaged into an instance of the result type, just as specified above.
If a query has no explicit
fromclause, and the given result type is an entity type, the root entity is inferred to be the result type.Passing
Object.classas the query result type is not recommended. In this special case, this method has the same semantics as the overloadcreateSelectionQuery(String).The returned
Querymay be executed by callingQuery.getResultList()orQuery.getSingleResult().- Parameters:
hqlString- The HQLselectquery as a stringresultType- TheClassobject representing the query result type, which should not beObject.class- Throws:
IllegalSelectQueryException- if the given HQL query is aninsert,updateordeletestatement- See Also:
- If the query has a single item in the
-
createSelectionQuery
Create aSelectionQueryinstance for the given HQL query string and givenEntityGraph, which is interpreted as a load graph. The query result type is the root entity of the given graph.- If the query has an explicit
selectclause, there must be a single item in theselectlist, and the select item must be assignable to the root type of the given graph. - Otherwise, if a query has no explicit
selectlist, the select list is inferred from the given entity graph. The query must have exactly one root entity in thefromclause, it must be assignable to the root type of the given graph, and the inferred select list will contain just that entity.
If a query has no explicit
fromclause, and the given result type is an entity type, the root entity is inferred to be the result type.The returned
Querymay be executed by callingQuery.getResultList()orQuery.getSingleResult().- Parameters:
hqlString- The HQLselectquery as a stringresultGraph- AnEntityGraphwhose root type is the query result type, which is interpreted as a load graph- Throws:
IllegalSelectQueryException- if the given HQL query is aninsert,updateordeletestatement- Since:
- 7.0
- See Also:
- If the query has an explicit
-
createSelectionQuery
Create aSelectionQueryreference for the givenCriteriaQuery.- See Also:
-
createMutationQuery
Create aMutationQueryreference for the given HQL insert, update, or delete statement.- Parameters:
hqlString- The HQLinsert,update, ordeletestatement- Throws:
IllegalMutationQueryException- if the given HQL query is aselectquery
-
createMutationQuery
Create aMutationQueryfrom the given update criteria tree -
createMutationQuery
Create aMutationQueryfrom the given delete criteria tree -
createMutationQuery
Create aMutationQueryfrom the given insert criteria tree -
createNativeMutationQuery
Create aNativeQueryinstance for the given native SQL statement.- Parameters:
sqlString- a native SQL statement string- Returns:
- The NativeQuery instance for manipulation and execution
-
createNamedQuery
Deprecated.Create a typedQueryinstance for the given named query. The named query might be defined in HQL or in native SQL.- Parameters:
name- the name of a predefined named query- Returns:
- The
Queryinstance for manipulation and execution - Throws:
IllegalArgumentException- if a query has not been defined with the given name or if the query string is found to be invalid- See Also:
-
createNamedQuery
Create a typedQueryinstance for the given named query. The named query might be defined in HQL or in native SQL.- Parameters:
name- the name of a query defined in metadataresultClass- the type of the query result- Returns:
- The
Queryinstance for manipulation and execution - Throws:
IllegalArgumentException- if a query has not been defined with the given name or if the query string is found to be invalid or if the query result is found to not be assignable to the specified type- See Also:
-
createNamedSelectionQuery
Deprecated.Create aSelectionQueryinstance for the namedNamedQuery.- Throws:
IllegalSelectQueryException- if the given HQL query is not a select queryUnknownNamedQueryException- if no query has been defined with the given name- Implementation Note:
- This method interprets some queries with an implicit
selectlist in a quite unintuitive way. In some future release, this method will be modified to throw an exception when passed a query with a missingselect. For now, usecreateNamedSelectionQuery(String, Class)to avoid ambiguity.
-
createNamedSelectionQuery
Create aSelectionQueryinstance for the namedNamedQuerywith the given result type.- Throws:
IllegalSelectQueryException- if the given HQL query is not a select queryUnknownNamedQueryException- if no query has been defined with the given name
-
createNamedMutationQuery
Create aMutationQueryinstance for the given named insert, update, or delete HQL query. The named query might be defined as HQL) or native-SQL.- Throws:
IllegalMutationQueryException- if the given HQL query is a select queryUnknownNamedQueryException- if no query has been defined with the given name
-
getNamedQuery
Deprecated.Create aQueryinstance for the named query.- Parameters:
queryName- the name of a predefined named query- Returns:
- The
Queryinstance for manipulation and execution - Throws:
IllegalArgumentException- if a query has not been defined with the given name or if the query string is found to be invalid
-
getNamedNativeQuery
Deprecated.Get aNativeQueryinstance for a named native SQL query- Parameters:
name- The name of the predefined query- Returns:
- The
NativeQueryinstance for manipulation and execution
-
getNamedNativeQuery
Deprecated.Get aNativeQueryinstance for a named native SQL query- Parameters:
name- The name of the predefined query- Returns:
- The
NativeQueryinstance for manipulation and execution
-
createNamedQuery(String, Class)