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();
...
});
| Modifier and Type | Field and Description |
|---|---|
| private final JpaCriteriaQuery |
| Access | Constructor and Description |
|---|---|
| public | |
| public | |
| public | |
| public | |
| public | |
| public | |
| public | |
| public | |
| public | |
| public | |
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public JpaCriteriaQuery | createCountQuery()
Implements org. |
| public TypedQuery | |
| public SelectionQuery | |
| public JpaCriteriaQuery | distinct(boolean
boolean value specifying whether duplicate
results must be eliminated from the query result or
whether they must be retained distinct)Implements org. |
| public JpaCriteriaQuery | fetch(JpaExpression<? extends Number> fetch)
Implements org. |
| public JpaCriteriaQuery | fetch(JpaExpression<? extends Number> fetch, FetchClauseType fetchClauseType)
Implements org. |
| public JpaCriteriaQuery | |
| public JpaCriteriaQuery | fetch(Number fetch, FetchClauseType fetchClauseType)
Implements org. |
| public <X> JpaRoot | from(Class<X>
the entity class entityClass)Implements org. |
| public <X> JpaRoot | from(EntityType<X>
metamodel entity representing the entity
of type X entity)Implements org. |
| public <X> JpaDerivedRoot | from(Subquery<X>
the subquery subquery)Implements org. |
| public <X> JpaRoot | from(JpaCteCriteria<X>
the cte criteria cte)Implements org. |
| public <T> JpaCteCriteria | getCteCriteria(String cteName)
Implements org. |
| public Collection | getCteCriterias()
Implements org. |
| public JpaExpression | |
| public FetchClauseType | |
| public List | getGroupList()
Implements jakarta. |
| public JpaPredicate | getGroupRestriction()
Implements jakarta. |
| public JpaExpression | |
| public List | getOrderList()
Overrides default org. |
| public Set | getParameters()
Implements org. |
| public JpaQueryPart | getQueryPart()
Implements org. |
| public JpaQueryStructure | getQuerySpec()
Implements org. |
| public JpaPredicate | getRestriction()
Implements jakarta. |
| public Class | getResultType()
Implements jakarta. |
| public List | getRootList()
Implements org. |
| public Set | |
| public JpaSelection | getSelection()
Implements jakarta. |
| public JpaCriteriaQuery | groupBy(Expression<T>...
zero or more grouping expressions grouping)Implements org. |
| public JpaCriteriaQuery | groupBy(List<Expression<?>>
list of zero or more grouping expressions grouping)Implements org. |
| public JpaCriteriaQuery | having(Expression<Boolean>
a simple or compound boolean expression restriction)Implements org. |
| public JpaCriteriaQuery | having(Predicate...
zero or more restriction predicates restrictions)Implements org. |
| public boolean | isDistinct()
Implements jakarta. |
| public JpaCriteriaQuery | multiselect(Selection<?>...
selection items corresponding to the
results to be returned by the query selections)Implements org. |
| public JpaCriteriaQuery | multiselect(List<Selection<?>>
list of selection items corresponding
to the results to be returned by the query list)Implements org. |
| public JpaCriteriaQuery | offset(JpaExpression<? extends Number> offset)
Implements org. |
| public JpaCriteriaQuery | |
| public JpaCriteriaQuery | orderBy(Order...
zero or more ordering expressions o)Implements org. |
| public JpaCriteriaQuery | orderBy(List<Order>
list of zero or more ordering expressions o)Implements org. |
| public JpaCriteriaQuery | |
| public JpaCriteriaQuery | select(Selection<? extends R>
selection specifying the item that
is to be returned in the query result selection)Implements org. |
| public <U> JpaSubQuery | subquery(Class<U>
the subquery result type type)Implements jakarta. |
| public JpaCriteriaQuery | where(Expression<Boolean>
a simple or compound boolean expression restriction)Implements org. |
| public JpaCriteriaQuery | where(Predicate...
zero or more restriction predicates restrictions)Implements org. |
| public <T> JpaCteCriteria | with(AbstractQuery<T> criteria)
Implements org. CriteriaQuery and returns a JpaCteCriteria,
which can be used for querying.
|
| public <T> JpaCteCriteria | with(String name, AbstractQuery<T> criteria)
Implements org. with(AbstractQuery) but assigns an explicit CTE name.
|
| public <T> JpaCteCriteria | withRecursiveUnionAll(AbstractQuery<T> baseCriteria, Function<JpaCteCriteria<T>, AbstractQuery<T>> recursiveCriteriaProducer)
Implements org. |
| public <T> JpaCteCriteria | withRecursiveUnionAll(String name, AbstractQuery<T> baseCriteria, Function<JpaCteCriteria<T>, AbstractQuery<T>> recursiveCriteriaProducer)
Implements org. withRecursiveUnionAll(AbstractQuery, Function) but assigns an explicit CTE name.
|
| public <T> JpaCteCriteria | withRecursiveUnionDistinct(AbstractQuery<T> baseCriteria, Function<JpaCteCriteria<T>, AbstractQuery<T>> recursiveCriteriaProducer)
Implements org. |
| public <T> JpaCteCriteria | withRecursiveUnionDistinct(String name, AbstractQuery<T> baseCriteria, Function<JpaCteCriteria<T>, AbstractQuery<T>> recursiveCriteriaProducer)
Implements org. withRecursiveUnionDistinct(AbstractQuery, Function) but assigns an explicit CTE name.
|