Interface SelectionSpecification<T>

Type Parameters:
T - The entity type returned by the query
All Superinterfaces:
QuerySpecification<T>

@Incubating public interface SelectionSpecification<T> extends QuerySpecification<T>
Specialization of QuerySpecification for programmatic customization of selection queries with ordering and restriction criteria.
  • The method restrict(Restriction) allows application of additional filtering to the query results. The static factory methods of Restriction are used to express filtering criteria of various kinds.
  • Refinement or replacement of the query sorting criteria is possible via the methods sort(Order) and resort(List), together with the static factory methods of Order.
  • The method fetch(Path) adds the path of an association to be fetched by the query.

Once all sorting and restrictions are specified, call createQuery() to obtain an executable selection query object.

 SelectionSpecification.create(Book.class,
             "from Book where discontinued = false")
         .restrict(Restriction.contains(Book_.title, "hibernate", false))
         .sort(Order.desc(Book_.title))
         .fetch(Path.from(Book.class).to(Book_publisher))
         .createQuery(session)                       // obtain a SelectionQuery
         .setPage(Page.first(50))
         .getResultList();
 

A SelectionSpecification always represents a query which returns a singe root entity. The restriction and ordering criteria are interpreted as applying to the field and properties of this root entity.

This interface, together with Order and Page, provides a streamlined API for offset-based pagination. For example, given a list of Orders in orderList, and the currentPage, we may write:

 SelectionSpecification.create(Book.class, "from Book where ... ")
         .resort(orderList)
         .createQuery(session)
                 .setPage(currentPage)
                 .getResultList();
 
Since:
7.0