Package org.hibernate.query.criteria
Class CriteriaDefinition<R>
- java.lang.Object
-
- org.hibernate.query.criteria.spi.HibernateCriteriaBuilderDelegate
-
- org.hibernate.query.criteria.CriteriaDefinition<R>
-
- Type Parameters:
R
- the query result type
- All Implemented Interfaces:
AbstractQuery<R>
,CommonAbstractCriteria
,CriteriaBuilder
,CriteriaQuery<R>
,Serializable
,HibernateCriteriaBuilder
,JpaCriteriaBase
,JpaCriteriaNode
,JpaCriteriaQuery<R>
,JpaCteContainer
,JpaQueryableCriteria<R>
,JpaSelectCriteria<R>
@Incubating public abstract class CriteriaDefinition<R> extends HibernateCriteriaBuilderDelegate implements JpaCriteriaQuery<R>
A utility class that makes it easier to build criteria queries. From within an initializer block of a (usually anonymous) subclass, all operations of theCriteriaBuilder
andCriteriaQuery
may be called without the need for specifying the target object.For example:
sessionFactory.inTransaction(session -> { List<Book> books = new CriteriaDefinition<>(sessionFactory, Book.class) {{ var book = from(Book.class); where(like(book.get(Book_.title), "%Hibernate%")); orderBy(desc(book.get(Book_.publicationDate)), asc(book.get(Book_.isbn))); book.fetch(Book_.authors); }} .createSelectionQuery(session) .setMaxResults(10) .getResultList(); ... });
A
CriteriaDefinition
may even be used to modify a base HQL or criteria query:sessionFactory.inTransaction(session -> { List<Book> books = new CriteriaDefinition<>(sessionFactory, Book.class, "from Book left join fetch authors where type = BOOK") {{ var book = (JpaRoot<Book>) getSelection(); where(getRestriction(), like(book.get(Book_.title), "%Hibernate%")); orderBy(desc(book.get(Book_.publicationDate)), asc(book.get(Book_.isbn))); }} .createSelectionQuery(session) .getResultList(); ... });
For queries which don't change between executions, theCriteriaDefinition
may be safely built and cached at startup:// build and cache the query static final CriteriaQuery<Book> bookQuery = new CriteriaDefinition<>(sessionFactory, Book.class) {{ var book = from(Book.class); where(like(book.get(Book_.title), "%Hibernate%")); orderBy(desc(book.get(Book_.publicationDate)), asc(book.get(Book_.isbn))); book.fetch(Book_.authors); }}; ... // execute it in a session sessionFactory.inTransaction(session -> { List<Book> books = session.createQuery(bookQuery) .setMaxResults(10) .getResultList(); ... });
- Since:
- 6.3
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface jakarta.persistence.criteria.CriteriaBuilder
CriteriaBuilder.Case<R extends Object>, CriteriaBuilder.Coalesce<T extends Object>, CriteriaBuilder.In<T extends Object>, CriteriaBuilder.SimpleCase<C extends Object,R extends Object>, CriteriaBuilder.Trimspec
-
-
Constructor Summary
Constructors Constructor Description CriteriaDefinition(EntityManagerFactory factory, CriteriaQuery<R> baseQuery)
CriteriaDefinition(EntityManagerFactory factory, Class<R> resultType)
CriteriaDefinition(EntityManagerFactory factory, Class<R> resultType, String baseHql)
CriteriaDefinition(EntityManager entityManager, CriteriaQuery<R> baseQuery)
CriteriaDefinition(EntityManager entityManager, Class<R> resultType)
CriteriaDefinition(EntityManager entityManager, Class<R> resultType, String baseHql)
CriteriaDefinition(SessionFactory factory, CriteriaQuery<R> baseQuery)
CriteriaDefinition(SessionFactory factory, Class<R> resultType)
CriteriaDefinition(SessionFactory factory, Class<R> resultType, String baseHql)
CriteriaDefinition(SharedSessionContract session, CriteriaQuery<R> baseQuery)
CriteriaDefinition(SharedSessionContract session, Class<R> resultType)
CriteriaDefinition(SharedSessionContract session, Class<R> resultType, String baseHql)
-
Method Summary
-
Methods inherited from class org.hibernate.query.criteria.spi.HibernateCriteriaBuilderDelegate
abs, acos, addDuration, addDuration, addDuration, all, and, and, any, array, array, array, array, asc, asc, asc, asc, asin, atan, atan2, atan2, atan2, avg, avg, avg, avg, between, between, cast, ceiling, coalesce, coalesce, coalesce, collate, concat, concat, concat, concat, conjunction, construct, construct, cos, cosh, count, count, count, count, countDistinct, createCriteriaDelete, createCriteriaInsertSelect, createCriteriaInsertValues, createCriteriaUpdate, createQuery, createQuery, createQuery, createTupleQuery, createWindow, cumeDist, currentDate, currentInstant, currentTime, currentTimestamp, day, degrees, denseRank, desc, desc, desc, desc, diff, diff, diff, disjunction, distinctFrom, distinctFrom, duration, durationBetween, durationBetween, durationByUnit, durationDiff, durationDiff, durationScaled, durationScaled, durationScaled, durationSum, durationSum, equal, equal, except, except, except, except, exceptAll, exceptAll, exists, exp, firstValue, fk, floor, format, frameBetweenFollowing, frameBetweenFollowing, frameBetweenPreceding, frameBetweenPreceding, frameCurrentRow, frameUnboundedFollowing, frameUnboundedPreceding, function, functionAggregate, functionAggregate, functionAggregate, functionWithinGroup, functionWithinGroup, functionWithinGroup, functionWithinGroup, ge, ge, getCriteriaBuilder, greaterThan, greaterThan, greaterThanOrEqualTo, greaterThanOrEqualTo, greatest, gt, gt, hour, ilike, ilike, ilike, ilike, ilike, ilike, in, in, in, in, indexes, intersect, intersect, intersect, intersect, intersectAll, intersectAll, isEmpty, isFalse, isMapEmpty, isMapNotEmpty, isMember, isMember, isNotEmpty, isNotMember, isNotMember, isNotNull, isNull, isTrue, keys, lastValue, le, le, least, left, left, length, lessThan, lessThan, lessThanOrEqualTo, lessThanOrEqualTo, like, like, like, like, like, like, listagg, listagg, listagg, listagg, listagg, listagg, listagg, listagg, literal, literals, literals, ln, localDate, localDateTime, localTime, locate, locate, locate, locate, log, log, log10, lower, lt, lt, mapSize, mapSize, max, min, minute, mod, mod, mod, mode, mode, mode, mode, month, neg, not, notDistinctFrom, notDistinctFrom, notEqual, notEqual, notIlike, notIlike, notIlike, notIlike, notIlike, notIlike, notLike, notLike, notLike, notLike, notLike, notLike, nthValue, nthValue, nullif, nullif, nullLiteral, or, or, overlay, overlay, overlay, overlay, overlay, overlay, overlay, overlay, overlay, overlay, overlay, overlay, pad, pad, pad, pad, pad, pad, pad, pad, pad, pad, pad, pad, parameter, parameter, percentileCont, percentileCont, percentileCont, percentileCont, percentileDisc, percentileDisc, percentileDisc, percentileDisc, percentRank, percentRank, percentRank, percentRank, percentRank, pi, power, power, prod, prod, prod, quot, quot, quot, radians, rank, rank, rank, rank, rank, repeat, repeat, repeat, replace, replace, replace, replace, right, right, round, rowNumber, search, search, search, second, selectCase, selectCase, sign, sin, sinh, size, size, some, sort, sort, sort, sql, sqrt, substring, substring, substring, substring, subtractDuration, subtractDuration, subtractDuration, sum, sum, sum, sum, sum, sum, sum, sumAsDouble, sumAsLong, tan, tanh, toBigDecimal, toBigInteger, toDouble, toFloat, toInteger, toLong, toString, treat, treat, treat, treat, treat, treat, treat, trim, trim, trim, trim, trim, trim, truncate, truncate, tuple, tuple, union, union, union, union, unionAll, unionAll, unwrap, upper, value, values, values, windowFunction, wrap, wrap, year
-
-
-
-
Constructor Detail
-
CriteriaDefinition
public CriteriaDefinition(SessionFactory factory, Class<R> resultType)
-
CriteriaDefinition
public CriteriaDefinition(SessionFactory factory, Class<R> resultType, String baseHql)
-
CriteriaDefinition
public CriteriaDefinition(SessionFactory factory, CriteriaQuery<R> baseQuery)
-
CriteriaDefinition
public CriteriaDefinition(EntityManagerFactory factory, Class<R> resultType)
-
CriteriaDefinition
public CriteriaDefinition(EntityManagerFactory factory, Class<R> resultType, String baseHql)
-
CriteriaDefinition
public CriteriaDefinition(EntityManagerFactory factory, CriteriaQuery<R> baseQuery)
-
CriteriaDefinition
public CriteriaDefinition(SharedSessionContract session, Class<R> resultType)
-
CriteriaDefinition
public CriteriaDefinition(SharedSessionContract session, Class<R> resultType, String baseHql)
-
CriteriaDefinition
public CriteriaDefinition(SharedSessionContract session, CriteriaQuery<R> baseQuery)
-
CriteriaDefinition
public CriteriaDefinition(EntityManager entityManager, Class<R> resultType)
-
CriteriaDefinition
public CriteriaDefinition(EntityManager entityManager, Class<R> resultType, String baseHql)
-
CriteriaDefinition
public CriteriaDefinition(EntityManager entityManager, CriteriaQuery<R> baseQuery)
-
-
Method Detail
-
createSelectionQuery
public SelectionQuery<R> createSelectionQuery(QueryProducer session)
-
createQuery
public TypedQuery<R> createQuery(EntityManager entityManager)
-
restrict
@Incubating public JpaCriteriaQuery<R> restrict(Predicate predicate)
-
select
public JpaCriteriaQuery<R> select(Selection<? extends R> selection)
- Specified by:
select
in interfaceCriteriaQuery<R>
- Specified by:
select
in interfaceJpaCriteriaQuery<R>
-
multiselect
public JpaCriteriaQuery<R> multiselect(Selection<?>... selections)
- Specified by:
multiselect
in interfaceCriteriaQuery<R>
- Specified by:
multiselect
in interfaceJpaCriteriaQuery<R>
-
multiselect
public JpaCriteriaQuery<R> multiselect(List<Selection<?>> list)
- Specified by:
multiselect
in interfaceCriteriaQuery<R>
- Specified by:
multiselect
in interfaceJpaCriteriaQuery<R>
-
where
public JpaCriteriaQuery<R> where(Expression<Boolean> restriction)
- Specified by:
where
in interfaceAbstractQuery<R>
- Specified by:
where
in interfaceCriteriaQuery<R>
- Specified by:
where
in interfaceJpaCriteriaQuery<R>
- Specified by:
where
in interfaceJpaSelectCriteria<R>
-
where
public JpaCriteriaQuery<R> where(Predicate... restrictions)
- Specified by:
where
in interfaceAbstractQuery<R>
- Specified by:
where
in interfaceCriteriaQuery<R>
- Specified by:
where
in interfaceJpaCriteriaQuery<R>
- Specified by:
where
in interfaceJpaSelectCriteria<R>
-
groupBy
public JpaCriteriaQuery<R> groupBy(Expression... grouping)
- Specified by:
groupBy
in interfaceAbstractQuery<R>
- Specified by:
groupBy
in interfaceCriteriaQuery<R>
- Specified by:
groupBy
in interfaceJpaCriteriaQuery<R>
- Specified by:
groupBy
in interfaceJpaSelectCriteria<R>
-
groupBy
public JpaCriteriaQuery<R> groupBy(List<Expression<?>> grouping)
- Specified by:
groupBy
in interfaceAbstractQuery<R>
- Specified by:
groupBy
in interfaceCriteriaQuery<R>
- Specified by:
groupBy
in interfaceJpaCriteriaQuery<R>
- Specified by:
groupBy
in interfaceJpaSelectCriteria<R>
-
having
public JpaCriteriaQuery<R> having(Expression<Boolean> restriction)
- Specified by:
having
in interfaceAbstractQuery<R>
- Specified by:
having
in interfaceCriteriaQuery<R>
- Specified by:
having
in interfaceJpaCriteriaQuery<R>
- Specified by:
having
in interfaceJpaSelectCriteria<R>
-
having
public JpaCriteriaQuery<R> having(Predicate... restrictions)
- Specified by:
having
in interfaceAbstractQuery<R>
- Specified by:
having
in interfaceCriteriaQuery<R>
- Specified by:
having
in interfaceJpaCriteriaQuery<R>
- Specified by:
having
in interfaceJpaSelectCriteria<R>
-
orderBy
public JpaCriteriaQuery<R> orderBy(Order... o)
- Specified by:
orderBy
in interfaceCriteriaQuery<R>
- Specified by:
orderBy
in interfaceJpaCriteriaQuery<R>
-
orderBy
public JpaCriteriaQuery<R> orderBy(List<Order> o)
- Specified by:
orderBy
in interfaceCriteriaQuery<R>
- Specified by:
orderBy
in interfaceJpaCriteriaQuery<R>
-
distinct
public JpaCriteriaQuery<R> distinct(boolean distinct)
- Specified by:
distinct
in interfaceAbstractQuery<R>
- Specified by:
distinct
in interfaceCriteriaQuery<R>
- Specified by:
distinct
in interfaceJpaCriteriaQuery<R>
- Specified by:
distinct
in interfaceJpaSelectCriteria<R>
-
getOrderList
public List<Order> getOrderList()
- Specified by:
getOrderList
in interfaceCriteriaQuery<R>
- Specified by:
getOrderList
in interfaceJpaCriteriaQuery<R>
-
getParameters
public Set<ParameterExpression<?>> getParameters()
Description copied from interface:JpaCriteriaQuery
- Specified by:
getParameters
in interfaceCriteriaQuery<R>
- Specified by:
getParameters
in interfaceJpaCriteriaQuery<R>
-
from
public <X> JpaRoot<X> from(Class<X> entityClass)
- Specified by:
from
in interfaceAbstractQuery<R>
- Specified by:
from
in interfaceJpaCriteriaQuery<R>
- Specified by:
from
in interfaceJpaSelectCriteria<R>
-
from
public <X> JpaRoot<X> from(EntityType<X> entity)
- Specified by:
from
in interfaceAbstractQuery<R>
- Specified by:
from
in interfaceJpaCriteriaQuery<R>
- Specified by:
from
in interfaceJpaSelectCriteria<R>
-
subquery
public <U> JpaSubQuery<U> subquery(Class<U> type)
- Specified by:
subquery
in interfaceCommonAbstractCriteria
- Specified by:
subquery
in interfaceJpaCriteriaBase
-
getRoots
public Set<Root<?>> getRoots()
- Specified by:
getRoots
in interfaceAbstractQuery<R>
-
getSelection
public JpaSelection<R> getSelection()
- Specified by:
getSelection
in interfaceAbstractQuery<R>
- Specified by:
getSelection
in interfaceJpaSelectCriteria<R>
-
getGroupList
public List<Expression<?>> getGroupList()
- Specified by:
getGroupList
in interfaceAbstractQuery<R>
-
getGroupRestriction
public JpaPredicate getGroupRestriction()
- Specified by:
getGroupRestriction
in interfaceAbstractQuery<R>
- Specified by:
getGroupRestriction
in interfaceJpaSelectCriteria<R>
-
isDistinct
public boolean isDistinct()
- Specified by:
isDistinct
in interfaceAbstractQuery<R>
-
getResultType
public Class<R> getResultType()
- Specified by:
getResultType
in interfaceAbstractQuery<R>
-
getRestriction
public JpaPredicate getRestriction()
- Specified by:
getRestriction
in interfaceCommonAbstractCriteria
- Specified by:
getRestriction
in interfaceJpaCriteriaBase
- Specified by:
getRestriction
in interfaceJpaSelectCriteria<R>
-
getOffset
public JpaExpression<Number> getOffset()
- Specified by:
getOffset
in interfaceJpaCriteriaQuery<R>
-
offset
public JpaCriteriaQuery<R> offset(JpaExpression<? extends Number> offset)
- Specified by:
offset
in interfaceJpaCriteriaQuery<R>
-
offset
public JpaCriteriaQuery<R> offset(Number offset)
- Specified by:
offset
in interfaceJpaCriteriaQuery<R>
-
getFetch
public JpaExpression<Number> getFetch()
- Specified by:
getFetch
in interfaceJpaCriteriaQuery<R>
-
fetch
public JpaCriteriaQuery<R> fetch(JpaExpression<? extends Number> fetch)
- Specified by:
fetch
in interfaceJpaCriteriaQuery<R>
-
fetch
public JpaCriteriaQuery<R> fetch(JpaExpression<? extends Number> fetch, FetchClauseType fetchClauseType)
- Specified by:
fetch
in interfaceJpaCriteriaQuery<R>
-
fetch
public JpaCriteriaQuery<R> fetch(Number fetch)
- Specified by:
fetch
in interfaceJpaCriteriaQuery<R>
-
fetch
public JpaCriteriaQuery<R> fetch(Number fetch, FetchClauseType fetchClauseType)
- Specified by:
fetch
in interfaceJpaCriteriaQuery<R>
-
getFetchClauseType
public FetchClauseType getFetchClauseType()
- Specified by:
getFetchClauseType
in interfaceJpaCriteriaQuery<R>
-
getRootList
public List<Root<?>> getRootList()
Description copied from interface:JpaCriteriaQuery
Return the roots as a list.- Specified by:
getRootList
in interfaceJpaCriteriaQuery<R>
-
getCteCriterias
public Collection<? extends JpaCteCriteria<?>> getCteCriterias()
Description copied from interface:JpaCteContainer
Returns the CTEs that are registered on this container.- Specified by:
getCteCriterias
in interfaceJpaCteContainer
-
getCteCriteria
public <T> JpaCteCriteria<T> getCteCriteria(String cteName)
Description copied from interface:JpaCteContainer
Returns a CTE that is registered by the given name on this container, or any of its parents.- Specified by:
getCteCriteria
in interfaceJpaCteContainer
-
with
public <T> JpaCteCriteria<T> with(AbstractQuery<T> criteria)
Description copied from interface:JpaCteContainer
Registers the givenCriteriaQuery
and returns aJpaCteCriteria
, which can be used for querying.- Specified by:
with
in interfaceJpaCteContainer
- See Also:
JpaSelectCriteria.from(JpaCteCriteria)
,JpaFrom.join(JpaCteCriteria, SqmJoinType)
-
withRecursiveUnionAll
public <T> JpaCteCriteria<T> withRecursiveUnionAll(AbstractQuery<T> baseCriteria, Function<JpaCteCriteria<T>,AbstractQuery<T>> recursiveCriteriaProducer)
Description copied from interface:JpaCteContainer
Allows to register a recursive CTE. The baseCriteriaQuery
serves for the structure of theJpaCteCriteria
, which is made available in the recursive criteria producer function, so that the recursiveCriteriaQuery
is able to refer to the CTE again.- Specified by:
withRecursiveUnionAll
in interfaceJpaCteContainer
- See Also:
JpaSelectCriteria.from(JpaCteCriteria)
,JpaFrom.join(JpaCteCriteria, SqmJoinType)
-
withRecursiveUnionDistinct
public <T> JpaCteCriteria<T> withRecursiveUnionDistinct(AbstractQuery<T> baseCriteria, Function<JpaCteCriteria<T>,AbstractQuery<T>> recursiveCriteriaProducer)
Description copied from interface:JpaCteContainer
Allows to register a recursive CTE. The baseCriteriaQuery
serves for the structure of theJpaCteCriteria
, which is made available in the recursive criteria producer function, so that the recursiveCriteriaQuery
is able to refer to the CTE again.- Specified by:
withRecursiveUnionDistinct
in interfaceJpaCteContainer
- See Also:
JpaSelectCriteria.from(JpaCteCriteria)
,JpaFrom.join(JpaCteCriteria, SqmJoinType)
-
with
public <T> JpaCteCriteria<T> with(String name, AbstractQuery<T> criteria)
Description copied from interface:JpaCteContainer
LikeJpaCteContainer.with(AbstractQuery)
but assigns an explicit CTE name.- Specified by:
with
in interfaceJpaCteContainer
-
withRecursiveUnionAll
public <T> JpaCteCriteria<T> withRecursiveUnionAll(String name, AbstractQuery<T> baseCriteria, Function<JpaCteCriteria<T>,AbstractQuery<T>> recursiveCriteriaProducer)
Description copied from interface:JpaCteContainer
LikeJpaCteContainer.withRecursiveUnionAll(AbstractQuery, Function)
but assigns an explicit CTE name.- Specified by:
withRecursiveUnionAll
in interfaceJpaCteContainer
-
withRecursiveUnionDistinct
public <T> JpaCteCriteria<T> withRecursiveUnionDistinct(String name, AbstractQuery<T> baseCriteria, Function<JpaCteCriteria<T>,AbstractQuery<T>> recursiveCriteriaProducer)
Description copied from interface:JpaCteContainer
LikeJpaCteContainer.withRecursiveUnionDistinct(AbstractQuery, Function)
but assigns an explicit CTE name.- Specified by:
withRecursiveUnionDistinct
in interfaceJpaCteContainer
-
getQuerySpec
public JpaQueryStructure<R> getQuerySpec()
Description copied from interface:JpaSelectCriteria
The query structure. SeeJpaQueryStructure
for details- Specified by:
getQuerySpec
in interfaceJpaSelectCriteria<R>
-
getQueryPart
public JpaQueryPart<R> getQueryPart()
Description copied from interface:JpaSelectCriteria
The query structure. SeeJpaQueryStructure
for details- Specified by:
getQueryPart
in interfaceJpaSelectCriteria<R>
-
from
public <X> JpaDerivedRoot<X> from(Subquery<X> subquery)
Description copied from interface:JpaSelectCriteria
Create and add a query root corresponding to the given subquery, forming a cartesian product with any existing roots.- Specified by:
from
in interfaceJpaSelectCriteria<R>
- Parameters:
subquery
- the subquery- Returns:
- query root corresponding to the given subquery
-
from
public <X> JpaRoot<X> from(JpaCteCriteria<X> cte)
Description copied from interface:JpaSelectCriteria
Create and add a query root corresponding to the given cte, forming a cartesian product with any existing roots.- Specified by:
from
in interfaceJpaSelectCriteria<R>
- Parameters:
cte
- the cte criteria- Returns:
- query root corresponding to the given cte
-
-