Package org.hibernate.query

Interface SelectionQuery<R>

All Superinterfaces:
CommonQueryContract
All Known Subinterfaces:
NativeQuery<T>,NativeQueryImplementor<R>,ProcedureCallImplementor<R>,Query<R>,QueryImplementor<R>,SqmQueryImplementor<R>,SqmSelectionQuery<R>,SqmSelectionQueryImplementor<R>
All Known Implementing Classes:
AbstractQuery,AbstractSelectionQuery,DelegatingSqmSelectionQueryImplementor,NativeQueryImpl,ProcedureCallImpl,QuerySqmImpl,SqmSelectionQueryImpl

@Incubatingpublic interfaceSelectionQuery<R>extendsCommonQueryContract
Within the context of an activesession, an instance of this type represents an executable selection query, that is, aselect. It is a slimmed-down version ofQuery, providing only methods relevant to selection queries.

ASelectionQuery may be obtained from theSession by calling:

ASelectionQuery controls how a query is executed, and allows arguments to be bound to its parameters.

A query which returns multiple results should be executed viagetResultList():
 List<Book> books =         session.createSelectionQuery("from Book left join fetch authors where title like :title")                 .setParameter("title", title)                 .setMaxResults(50)                 .getResultList();
A query which is expected to return exactly one on result should be executed viagetSingleResult(), or, if it might not return a result,getSingleResultOrNull():
 Book book =         session.createSelectionQuery("from Book where isbn = ?1")                 .setParameter(1, isbn)                 .getSingleResultOrNull();

A query may have explicitfetch joins, specified using the syntaxjoin fetch in HQL, or viaFetchParent.fetch(jakarta.persistence.metamodel.SingularAttribute<? super X, Y>) in the criteria API. Additional fetch joins may be added by:

The special built-in fetch profile named"org.hibernate.defaultProfile" adds a fetch join for everyeager@ManyToOne or@OneToOne association belonging to an entity returned by the query.

Finally, three alternative approaches to pagination are available:

  1. The ancient but dependable operationssetFirstResult(int) andsetMaxResults(int) are the standard approach blessed by the JPA specification.
  2. SelectionSpecification andsetPage(Page), together withOrder andPage, provide a streamlined API for offset-based pagination, at a slightly higher semantic level.
  3. On the other hand,KeyedPage andKeyedResultList, along withgetKeyedResultList(KeyedPage), provide forkey-based pagination, which can help eliminate missed or duplicate results when data is modified between page requests.