Interface SelectionSpecification<T>
- Type Parameters:
T- The entity type returned by the query
- All Superinterfaces:
QuerySpecification<T>
- All Known Implementing Classes:
SelectionSpecificationImpl
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 ofRestrictionare used to express filtering criteria of various kinds. - Refinement or replacement of the query sorting criteria is possible via the methods
sort(Order)andresort(List), together with the static factory methods ofOrder. - 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
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceA function capable of modifying or augmenting a criteria query. -
Method Summary
Modifier and TypeMethodDescriptionaugment(SelectionSpecification.Augmentation<T> augmentation) Add an augmentation to the specification.buildCriteria(CriteriaBuilder builder) Build acriteria querysatisfying this specification, using the givenCriteriaBuilder.static <T> SelectionSpecification<T>create(CriteriaQuery<T> criteria) Returns a specification reference which can be used to programmatically, iteratively build a SelectionQuery for the given criteria query, allowing the addition of sorting and restrictions.static <T> SelectionSpecification<T>Returns a specification reference which can be used to programmatically, iteratively build a SelectionQuery for the given entity type, allowing the addition of sorting and restrictions.static <T> SelectionSpecification<T>Returns a specification reference which can be used to programmatically, iteratively build a SelectionQuery based on a base HQL statement, allowing the addition of sorting and restrictions.createQuery(EntityManager entityManager) Finalize the building and create executable query instance.createQuery(Session session) Finalize the building and create executable query instance.createQuery(StatelessSession session) Finalize the building and create executable query instance.Add a fetch path to the specification.Obtain a reference to this specification which may be passed along toEntityManager.createQuery(TypedQueryReference).Sets the sorting for this selection specification.Sets the ordering for this selection specification.restrict(Restriction<? super T> restriction) Adds a restriction to the query specification.Adds an ordering to the selection specification.validate(CriteriaBuilder builder) Validate the query.
-
Method Details
-
sort
Adds an ordering to the selection specification. Appended to any previous ordering.- Parameters:
order- The ordering fragment to be added.- Returns:
thisfor method chaining.
-
resort
Sets the ordering for this selection specification. If ordering was already defined, this method drops the previous ordering in favor of the passedorders.- Parameters:
order- The ordering fragment to be used.- Returns:
thisfor method chaining.
-
resort
Sets the sorting for this selection specification. If sorting was already defined, this method drops the previous sorting in favor of the passedorders.- Parameters:
orders- The sorting fragments to be used.- Returns:
thisfor method chaining.
-
restrict
Description copied from interface:QuerySpecificationAdds a restriction to the query specification.- Specified by:
restrictin interfaceQuerySpecification<T>- Parameters:
restriction- The restriction predicate to be added.- Returns:
thisfor method chaining.
-
fetch
Add a fetch path to the specification.- Parameters:
fetchPath- The path to fetch- Returns:
thisfor method chaining.
-
augment
Add an augmentation to the specification.For example:
SelectionSpecification.create(Book.class) .augment((builder, query, book) -> // augment the query via JPA Criteria API query.where(builder.like(book.get(Book_.title), titlePattern)), builder.greaterThan(book.get(Book_.pages), minPages)) .orderBy(builder.asc(book.get(Book_.isbn))) .createQuery(session) .getResultList();For complicated cases, aCriteriaDefinitionmay be used within an augmentation to eliminate repetitive explicit references to theCriteriaBuilder.SelectionSpecification.create(Book.class) .augment((builder, query, book) -> // eliminate explicit references to 'builder' new CriteriaDefinition<>(query) {{ where(like(entity.get(BasicEntity_.title), titlePattern), greaterThan(book.get(Book_.pages), minPages)); orderBy(asc(book.get(Book_.isbn))); }} ) .createQuery(session) .getResultList();- Parameters:
augmentation- A function capable of modifying or augmenting a criteria query.- Returns:
thisfor method chaining.
-
createQuery
Description copied from interface:QuerySpecificationFinalize the building and create executable query instance.- Specified by:
createQueryin interfaceQuerySpecification<T>
-
createQuery
Description copied from interface:QuerySpecificationFinalize the building and create executable query instance.- Specified by:
createQueryin interfaceQuerySpecification<T>
-
createQuery
Description copied from interface:QuerySpecificationFinalize the building and create executable query instance.- Specified by:
createQueryin interfaceQuerySpecification<T>
-
buildCriteria
Build acriteria querysatisfying this specification, using the givenCriteriaBuilder.If the returned criteria query is mutated, the mutations will not be not reflected in this specification.
- Specified by:
buildCriteriain interfaceQuerySpecification<T>- Returns:
- a new criteria query
-
validate
Description copied from interface:QuerySpecificationValidate the query.- Specified by:
validatein interfaceQuerySpecification<T>- Returns:
thisif everything is fine
-
reference
TypedQueryReference<T> reference()Description copied from interface:QuerySpecificationObtain a reference to this specification which may be passed along toEntityManager.createQuery(TypedQueryReference).- Specified by:
referencein interfaceQuerySpecification<T>
-
create
Returns a specification reference which can be used to programmatically, iteratively build a SelectionQuery for the given entity type, allowing the addition of sorting and restrictions. This is effectively the same as calling create(Class, String) with"from {rootEntityType}"as the HQL.- Type Parameters:
T- The entity type which is the root of the query.resultTypeand<T>are both expected to refer to a singular query root.- Parameters:
rootEntityType- The entity type which is the root of the query.
-
create
Returns a specification reference which can be used to programmatically, iteratively build a SelectionQuery based on a base HQL statement, allowing the addition of sorting and restrictions.- Type Parameters:
T- The root entity type for the query.resultTypeand<T>are both expected to refer to a singular query root.- Parameters:
hql- The base HQL query.resultType- The result type which will ultimately be returned from the SelectionQuery- Throws:
IllegalSelectQueryException- The given HQL is expected to be aselectquery. This method will throw an exception if not.
-
create
Returns a specification reference which can be used to programmatically, iteratively build a SelectionQuery for the given criteria query, allowing the addition of sorting and restrictions.- Type Parameters:
T- The entity type which is the root of the query.- Parameters:
criteria- The criteria query
-