Top Description Fields Constructors Methods
org.hibernate.query.criteria

public abstract Class CriteriaDefinition<R>

extends HibernateCriteriaBuilderDelegate
implements JpaCriteriaQuery<R>
Class Inheritance
All Implemented Interfaces
org.hibernate.query.criteria.JpaCriteriaQuery, org.hibernate.query.criteria.JpaSelectCriteria, org.hibernate.query.criteria.JpaCriteriaBase, org.hibernate.query.criteria.JpaCriteriaNode, java.io.Serializable, jakarta.persistence.criteria.CommonAbstractCriteria, jakarta.persistence.criteria.AbstractQuery, org.hibernate.query.criteria.JpaQueryableCriteria, org.hibernate.query.criteria.JpaCteContainer, jakarta.persistence.criteria.CriteriaQuery
Annotations
@Incubating
Type Parameters
<R>
the query result type
Imports
jakarta.persistence.EntityManager, .EntityManagerFactory, .TypedQuery, jakarta.persistence.criteria.*, jakarta.persistence.metamodel.EntityType, org.hibernate.Incubating, .SessionFactory, .SharedSessionContract, org.hibernate.query.QueryProducer, .SelectionQuery, org.hibernate.query.criteria.spi.HibernateCriteriaBuilderDelegate, org.hibernate.query.sqm.FetchClauseType, java.util.Collection, .List, .Set, java.util.function.Function

A utility class that makes it easier to build criteria queries. From within an initializer block of a (usually anonymous) subclass, all operations of the CriteriaBuilder and CriteriaQuery 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, the CriteriaDefinition 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();
    ...
});
Author
Gavin King
Since
6.3

Field Summary

Modifier and TypeField and Description
private final JpaCriteriaQuery<R>

Constructor Summary

AccessConstructor and Description
public
CriteriaDefinition(SessionFactory factory, Class<R> resultType)

public
CriteriaDefinition(SessionFactory factory, Class<R> resultType, String baseHql)

public
public
public
CriteriaDefinition(EntityManagerFactory factory, Class<R> resultType, String baseHql)

public
public
public
CriteriaDefinition(SharedSessionContract session, Class<R> resultType, String baseHql)

public
public
CriteriaDefinition(EntityManager entityManager, Class<R> resultType)

public
CriteriaDefinition(EntityManager entityManager, Class<R> resultType, String baseHql)

public
CriteriaDefinition(EntityManager entityManager, CriteriaQuery<R> baseQuery)

Method Summary

Modifier and TypeMethod and Description
public JpaCriteriaQuery<Long>
createCountQuery()

Implements org.hibernate.query.criteria.JpaCriteriaQuery.createCountQuery.

Wraps this query in a subquery and returns a count query based on that subquery in the from clause.
public TypedQuery<R>
createQuery(EntityManager entityManager)

public SelectionQuery<R>
public JpaCriteriaQuery<R>
distinct(boolean
boolean value specifying whether duplicate results must be eliminated from the query result or whether they must be retained
distinct
)

Implements org.hibernate.query.criteria.JpaCriteriaQuery.distinct.

Specify whether duplicate query results will be eliminated.
public JpaCriteriaQuery<R>
public JpaCriteriaQuery<R>
public JpaCriteriaQuery<R>
public JpaCriteriaQuery<R>
public <X> JpaRoot<X>
from(Class<X>
the entity class
entityClass
)

Implements org.hibernate.query.criteria.JpaCriteriaQuery.from.

Create and add a query root corresponding to the given entity, forming a cartesian product with any existing roots.
public <X> JpaRoot<X>
from(EntityType<X>
metamodel entity representing the entity of type X
entity
)

Implements org.hibernate.query.criteria.JpaCriteriaQuery.from.

Create and add a query root corresponding to the given entity, forming a cartesian product with any existing roots.
public <X> JpaDerivedRoot<X>
from(Subquery<X>
the subquery
subquery
)

Implements org.hibernate.query.criteria.JpaSelectCriteria.from.

Create and add a query root corresponding to the given subquery, forming a cartesian product with any existing roots.
public <X> JpaRoot<X>
from(JpaCteCriteria<X>
the cte criteria
cte
)

Implements org.hibernate.query.criteria.JpaSelectCriteria.from.

Create and add a query root corresponding to the given cte, forming a cartesian product with any existing roots.
public <T> JpaCteCriteria<T>
getCteCriteria(String cteName)

Implements org.hibernate.query.criteria.JpaCteContainer.getCteCriteria.

Returns a CTE that is registered by the given name on this container, or any of its parents.
public Collection<? extends JpaCteCriteria<?>>
getCteCriterias()

Implements org.hibernate.query.criteria.JpaCteContainer.getCteCriterias.

Returns the CTEs that are registered on this container.
public JpaExpression<Number>
public FetchClauseType
public List<Expression<?>>
getGroupList()

Implements jakarta.persistence.criteria.AbstractQuery.getGroupList.

Return a list of the grouping expressions.
public JpaPredicate
getGroupRestriction()

Implements jakarta.persistence.criteria.AbstractQuery.getGroupRestriction, org.hibernate.query.criteria.JpaSelectCriteria.getGroupRestriction.

Return the predicate that corresponds to the restriction(s) over the grouping items, or null if no restrictions have been specified.
public JpaExpression<Number>
public List<Order>
getOrderList()

Overrides default org.hibernate.query.criteria.JpaCriteriaQuery.getOrderList.

Return the ordering expressions in order of precedence.
public Set<ParameterExpression<?>>
getParameters()

Implements org.hibernate.query.criteria.JpaCriteriaQuery.getParameters.

Return the parameters of the query.
public JpaQueryPart<R>
public JpaQueryStructure<R>
public JpaPredicate
getRestriction()

Implements jakarta.persistence.criteria.CommonAbstractCriteria.getRestriction, org.hibernate.query.criteria.JpaCriteriaBase.getRestriction, org.hibernate.query.criteria.JpaSelectCriteria.getRestriction.

Return the predicate that corresponds to the where clause restriction(s), or null if no restrictions have been specified.
public Class<R>
getResultType()

Implements jakarta.persistence.criteria.AbstractQuery.getResultType.

Return the result type of the query or subquery.
public List<Root<?>>
public Set<Root<?>>
public JpaSelection<R>
getSelection()

Implements jakarta.persistence.criteria.AbstractQuery.getSelection, org.hibernate.query.criteria.JpaSelectCriteria.getSelection.

Return the selection of the query, or null if no selection has been set.
public JpaCriteriaQuery<R>
groupBy(Expression<T>...
zero or more grouping expressions
grouping
)

Implements org.hibernate.query.criteria.JpaCriteriaQuery.groupBy.

Specify the expressions that are used to form groups over the query results.
public JpaCriteriaQuery<R>
groupBy(List<Expression<?>>
list of zero or more grouping expressions
grouping
)

Implements org.hibernate.query.criteria.JpaCriteriaQuery.groupBy.

Specify the expressions that are used to form groups over the query results.
public JpaCriteriaQuery<R>
having(Expression<Boolean>
a simple or compound boolean expression
restriction
)

Implements org.hibernate.query.criteria.JpaCriteriaQuery.having.

Specify a restriction over the groups of the query.
public JpaCriteriaQuery<R>
having(Predicate...
zero or more restriction predicates
restrictions
)

Implements org.hibernate.query.criteria.JpaCriteriaQuery.having.

Specify restrictions over the groups of the query according the conjunction of the specified restriction predicates.
public boolean
isDistinct()

Implements jakarta.persistence.criteria.AbstractQuery.isDistinct.

Return whether duplicate query results must be eliminated or retained.
public JpaCriteriaQuery<R>
multiselect(Selection<?>...
selection items corresponding to the results to be returned by the query
selections
)

Implements org.hibernate.query.criteria.JpaCriteriaQuery.multiselect.

Specify the selection items that are to be returned in the query result.
public JpaCriteriaQuery<R>
multiselect(List<Selection<?>>
list of selection items corresponding to the results to be returned by the query
list
)

Implements org.hibernate.query.criteria.JpaCriteriaQuery.multiselect.

Specify the selection items that are to be returned in the query result.
public JpaCriteriaQuery<R>
public JpaCriteriaQuery<R>
public JpaCriteriaQuery<R>
orderBy(Order...
zero or more ordering expressions
o
)

Implements org.hibernate.query.criteria.JpaCriteriaQuery.orderBy.

Specify the ordering expressions that are used to order the query results.
public JpaCriteriaQuery<R>
orderBy(List<Order>
list of zero or more ordering expressions
o
)

Implements org.hibernate.query.criteria.JpaCriteriaQuery.orderBy.

Specify the ordering expressions that are used to order the query results.
public JpaCriteriaQuery<R>
restrict(Predicate predicate)

public JpaCriteriaQuery<R>
select(Selection<? extends R>
selection specifying the item that is to be returned in the query result
selection
)

Implements org.hibernate.query.criteria.JpaCriteriaQuery.select.

Specify the item that is to be returned in the query result.
public <U> JpaSubQuery<U>
public JpaCriteriaQuery<R>
where(Expression<Boolean>
a simple or compound boolean expression
restriction
)

Implements org.hibernate.query.criteria.JpaCriteriaQuery.where.

Modify the query to restrict the query result according to the specified boolean expression.
public JpaCriteriaQuery<R>
where(Predicate...
zero or more restriction predicates
restrictions
)

Implements org.hibernate.query.criteria.JpaCriteriaQuery.where.

Modify the query to restrict the query result according to the conjunction of the specified restriction predicates.
public <T> JpaCteCriteria<T>
with(AbstractQuery<T> criteria)

Implements org.hibernate.query.criteria.JpaCteContainer.with.

Registers the given CriteriaQuery and returns a JpaCteCriteria, which can be used for querying.
public <T> JpaCteCriteria<T>
with(String name, AbstractQuery<T> criteria)

Implements org.hibernate.query.criteria.JpaCteContainer.with.

Like with(AbstractQuery) but assigns an explicit CTE name.
public <T> JpaCteCriteria<T>
withRecursiveUnionAll(AbstractQuery<T> baseCriteria, Function<JpaCteCriteria<T>, AbstractQuery<T>> recursiveCriteriaProducer)

Implements org.hibernate.query.criteria.JpaCteContainer.withRecursiveUnionAll.

Allows to register a recursive CTE.
public <T> JpaCteCriteria<T>
public <T> JpaCteCriteria<T>
withRecursiveUnionDistinct(AbstractQuery<T> baseCriteria, Function<JpaCteCriteria<T>, AbstractQuery<T>> recursiveCriteriaProducer)

Implements org.hibernate.query.criteria.JpaCteContainer.withRecursiveUnionDistinct.

Allows to register a recursive CTE.
public <T> JpaCteCriteria<T>
Inherited from org.hibernate.query.criteria.spi.HibernateCriteriaBuilderDelegate:
absacosaddDurationaddDurationaddDurationallandandanyarrayarrayarrayarrayarrayAggarrayAggarrayAggarrayAggarrayAppendarrayAppendarrayConcatarrayConcatarrayConcatarrayContainsarrayContainsarrayContainsarrayContainsAllarrayContainsAllarrayContainsAllarrayContainsAllNullablearrayContainsAllNullablearrayContainsAllNullablearrayContainsNullablearrayContainsNullablearrayContainsNullablearrayFillarrayFillarrayFillarrayFillarrayGetarrayGetarrayLengtharrayLiteralarrayOverlapsarrayOverlapsarrayOverlapsarrayOverlapsNullablearrayOverlapsNullablearrayOverlapsNullablearrayPositionarrayPositionarrayPositionsarrayPositionsarrayPositionsListarrayPositionsListarrayPrependarrayPrependarrayRemovearrayRemovearrayRemoveIndexarrayRemoveIndexarrayReplacearrayReplacearrayReplacearrayReplacearraySetarraySetarraySetarraySetarraySlicearraySlicearraySlicearraySlicearrayToStringarrayToStringarrayTrimarrayTrimascascascascasinatanatan2atan2atan2avgavgavgavgbetweenbetweencastceilingcoalescecoalescecoalescecollatecollectionAppendcollectionAppendcollectionConcatcollectionConcatcollectionConcatcollectionContainscollectionContainscollectionContainscollectionContainsAllcollectionContainsAllcollectionContainsAllcollectionContainsAllNullablecollectionContainsAllNullablecollectionContainsAllNullablecollectionContainsNullablecollectionContainsNullablecollectionContainsNullablecollectionFillcollectionFillcollectionFillcollectionFillcollectionGetcollectionGetcollectionLengthcollectionLiteralcollectionOverlapscollectionOverlapscollectionOverlapscollectionOverlapsNullablecollectionOverlapsNullablecollectionOverlapsNullablecollectionPositioncollectionPositioncollectionPositionscollectionPositionscollectionPositionsListcollectionPositionsListcollectionPrependcollectionPrependcollectionRemovecollectionRemovecollectionRemoveIndexcollectionRemoveIndexcollectionReplacecollectionReplacecollectionReplacecollectionReplacecollectionSetcollectionSetcollectionSetcollectionSetcollectionSlicecollectionSlicecollectionSlicecollectionSlicecollectionToStringcollectionToStringcollectionTrimcollectionTrimconcatconcatconcatconcatconjunctionconstructconstructcoscoshcountcountcountcountcountDistinctcreateCriteriaDeletecreateCriteriaInsertSelectcreateCriteriaInsertValuescreateCriteriaUpdatecreateQuerycreateQuerycreateQuerycreateTupleQuerycreateWindowcumeDistcurrentDatecurrentInstantcurrentTimecurrentTimestampdaydegreesdenseRankdescdescdescdescdiffdiffdiffdisjunctiondistinctFromdistinctFromdurationdurationBetweendurationBetweendurationByUnitdurationDiffdurationDiffdurationScaleddurationScaleddurationScaleddurationSumdurationSumequalequalexceptexceptexceptexceptexceptAllexceptAllexistsexpfirstValuefkfloorformatframeBetweenFollowingframeBetweenFollowingframeBetweenPrecedingframeBetweenPrecedingframeCurrentRowframeUnboundedFollowingframeUnboundedPrecedingfunctionfunctionAggregatefunctionAggregatefunctionAggregatefunctionWithinGroupfunctionWithinGroupfunctionWithinGroupfunctionWithinGroupgegegetCriteriaBuildergreaterThangreaterThangreaterThanOrEqualTogreaterThanOrEqualTogreatestgtgthourilikeilikeilikeilikeilikeilikeininininindexesintersectintersectintersectintersectintersectAllintersectAllisEmptyisFalseisMapEmptyisMapNotEmptyisMemberisMemberisNotEmptyisNotMemberisNotMemberisNotNullisNullisTruekeyslastValueleleleastleftleftlengthlessThanlessThanlessThanOrEqualTolessThanOrEqualTolikelikelikelikelikelikelistagglistagglistagglistagglistagglistagglistagglistaggliteralliteralsliteralslnlocalDatelocalDateTimelocalTimelocatelocatelocatelocatelogloglog10lowerltltmapSizemapSizemaxminminutemodmodmodmodemodemodemodemonthnegnotnotDistinctFromnotDistinctFromnotEqualnotEqualnotIlikenotIlikenotIlikenotIlikenotIlikenotIlikenotLikenotLikenotLikenotLikenotLikenotLikenthValuenthValuenullifnullifnullLiteralororoverlayoverlayoverlayoverlayoverlayoverlayoverlayoverlayoverlayoverlayoverlayoverlaypadpadpadpadpadpadpadpadpadpadpadpadparameterparameterpercentileContpercentileContpercentileContpercentileContpercentileDiscpercentileDiscpercentileDiscpercentileDiscpercentRankpercentRankpercentRankpercentRankpercentRankpipowerpowerprodprodprodquotquotquotradiansrankrankrankrankrankrepeatrepeatrepeatreplacereplacereplacereplacerightrightroundrowNumbersearchsearchsearchsecondselectCaseselectCasesignsinsinhsizesizesomesortsortsortsqlsqrtsubstringsubstringsubstringsubstringsubtractDurationsubtractDurationsubtractDurationsumsumsumsumsumsumsumsumAsDoublesumAsLongtantanhtoBigDecimaltoBigIntegertoDoubletoFloattoIntegertoLongtoStringtreattreattreattreattreattreattreattrimtrimtrimtrimtrimtrimtruncatetruncatetupletupleunionunionunionunionunionAllunionAllunwrapuppervaluevaluesvalueswindowFunctionwrapwrapyear