- All Implemented Interfaces:
jakarta.persistence.criteria.CriteriaBuilder
- Enclosing interface:
- QueryStream<X,
S extends jakarta.persistence.criteria.Selection<X>, C extends jakarta.persistence.criteria.CommonAbstractCriteria, C2 extends C, Q extends jakarta.persistence.Query>
QueryStreams, DeleteStreams, and UpdateStreams.
New instances of this class are created via QueryStream.newBuilder().
For convenience, this class also implements CriteriaBuilder.
The primary stream creation methods are:
stream()- Create aSearchStreamfor search queries.deleteStream()- Create aDeleteStreamfor bulk delete queries.updateStream()- Create aUpdateStreamfor bulk update queries.
The following methods create SearchStreams for use in correlated subqueries:
substream(Root)- Create a correlated subquerySearchStreamfrom aRoot.substream(Join)- Create a correlated subquerySearchStreamfrom aJoin.substream(SetJoin)- Create a correlated subquerySearchStreamfrom aSetJoin.substream(MapJoin)- Create a correlated subquerySearchStreamfrom aMapJoin.substream(ListJoin)- Create a correlated subquerySearchStreamfrom aListJoin.substream(CollectionJoin)- Create a correlated subquerySearchStreamfrom aCollectionJoin.substream(From)- Create a correlated subquerySearchStreamfrom anyFromwhen a more specific type is unknown.
See substream(Root) for an example of using substreams.
The following methods provide "convenience" access to objects that are not always readily available:
currentQuery()- Access the current Criteria API query under construction.bindParam()- Register a parameter binding with the currentQueryunder construction.getEntityManager()- Get theEntityManagerassociated with this instance.
-
Nested Class Summary
Nested classes/interfaces inherited from interface jakarta.persistence.criteria.CriteriaBuilder
jakarta.persistence.criteria.CriteriaBuilder.Case<R extends Object>, jakarta.persistence.criteria.CriteriaBuilder.Coalesce<T extends Object>, jakarta.persistence.criteria.CriteriaBuilder.In<T extends Object>, jakarta.persistence.criteria.CriteriaBuilder.SimpleCase<C extends Object,R extends Object>, jakarta.persistence.criteria.CriteriaBuilder.Trimspec -
Method Summary
Modifier and TypeMethodDescriptionvoidbindParam(ParamBinding<?> binding) Register a parameter binding with the currentQuerythat is under construction.jakarta.persistence.criteria.CommonAbstractCriteriaAccess the current Criteria API query under construction.<X> DeleteStream<X>deleteStream(Class<X> type) Create aDeleteStreamfor bulk delete queries.jakarta.persistence.criteria.CriteriaBuilderGet theCriteriaBuilderassociated with this instance.jakarta.persistence.EntityManagerGet theEntityManagerassociated with this instance.<X> RootStream<X>Create aSearchStreamfor search queries.<X,E> FromStream<E, jakarta.persistence.criteria.CollectionJoin<X, E>> substream(jakarta.persistence.criteria.CollectionJoin<X, E> join) Create aSearchStreamfor use as a subquery, using the specified join.<X,Y> FromStream<Y, ? extends jakarta.persistence.criteria.From<X, Y>> substream(jakarta.persistence.criteria.From<X, Y> from) Create aSearchStreamfor use as a subquery, using the specifiedFrom.<X,E> FromStream<E, jakarta.persistence.criteria.Join<X, E>> substream(jakarta.persistence.criteria.Join<X, E> join) Create aSearchStreamfor use as a subquery, using the specified join.<X,E> FromStream<E, jakarta.persistence.criteria.ListJoin<X, E>> substream(jakarta.persistence.criteria.ListJoin<X, E> join) Create aSearchStreamfor use as a subquery, using the specified join.<X,K, V> FromStream<V, jakarta.persistence.criteria.MapJoin<X, K, V>> substream(jakarta.persistence.criteria.MapJoin<X, K, V> join) Create aSearchStreamfor use as a subquery, using the specified join.<X> RootStream<X>substream(jakarta.persistence.criteria.Root<X> root) Create aSearchStreamfor use as a subquery, using the specified correlatedRoot.<X,E> FromStream<E, jakarta.persistence.criteria.SetJoin<X, E>> substream(jakarta.persistence.criteria.SetJoin<X, E> join) Create aSearchStreamfor use as a subquery, using the specified join.<X> UpdateStream<X>updateStream(Class<X> type) Create aUpdateStreamfor bulk update queries.Methods inherited from class org.dellroad.querystream.jpa.util.ForwardingCriteriaBuilder
abs, all, and, and, any, array, asc, avg, between, between, ceiling, coalesce, coalesce, coalesce, concat, concat, concat, conjunction, construct, count, countDistinct, createCriteriaDelete, createCriteriaUpdate, createQuery, createQuery, createTupleQuery, currentDate, currentTime, currentTimestamp, desc, diff, diff, diff, disjunction, equal, equal, exists, exp, floor, function, ge, ge, greaterThan, greaterThan, greaterThanOrEqualTo, greaterThanOrEqualTo, greatest, gt, gt, in, isEmpty, isFalse, isMember, isMember, isNotEmpty, isNotMember, isNotMember, isNotNull, isNull, isTrue, keys, le, le, least, length, lessThan, lessThan, lessThanOrEqualTo, lessThanOrEqualTo, like, like, like, like, like, like, literal, ln, localDate, localDateTime, localTime, locate, locate, locate, locate, lower, lt, lt, max, min, mod, mod, mod, neg, not, notEqual, notEqual, notLike, notLike, notLike, notLike, notLike, notLike, nullif, nullif, nullLiteral, or, or, parameter, parameter, power, power, prod, prod, prod, quot, quot, quot, round, selectCase, selectCase, sign, size, size, some, sqrt, substring, substring, substring, substring, sum, sum, sum, sum, sumAsDouble, sumAsLong, toBigDecimal, toBigInteger, toDouble, toFloat, toInteger, toLong, toString, treat, treat, treat, treat, treat, treat, treat, trim, trim, trim, trim, trim, trim, tuple, upper, values
-
Method Details
-
getEntityManager
public jakarta.persistence.EntityManager getEntityManager()Get theEntityManagerassociated with this instance.- Returns:
- associated
EntityManager
-
getCriteriaBuilder
public jakarta.persistence.criteria.CriteriaBuilder getCriteriaBuilder()Get theCriteriaBuilderassociated with this instance.- Specified by:
getCriteriaBuilderin classForwardingCriteriaBuilder- Returns:
CriteriaBuildercreated from this instance'sEntityManager
-
stream
Create aSearchStreamfor search queries.- Type Parameters:
X- stream result type- Parameters:
type- stream result type- Returns:
- new search stream
- Throws:
IllegalArgumentException- iftypeis null
-
substream
Create aSearchStreamfor use as a subquery, using the specified correlatedRoot.The returned
RootStreamcannot be materialized directly viatoQuery()ortoCriteriaQuery(); instead, it can only be used indirectly as a correlated subquery.Here's an example that returns the names of teachers who have one or more newly enrolled students:
List<String> names = qb.stream(Teacher.class) .filter(teacher -> qb.substream(teacher) .map(Teacher_.students) .filter(Student_.newlyEnrolled) .exists())) .map(Teacher_.name) .getResultList();- Type Parameters:
X- stream result type- Parameters:
root- correlated root for subquery- Returns:
- new subquery search stream
- Throws:
IllegalArgumentException- ifrootis null
-
substream
public <X,Y> FromStream<Y,? extends jakarta.persistence.criteria.From<X, substreamY>> (jakarta.persistence.criteria.From<X, Y> from) Create aSearchStreamfor use as a subquery, using the specifiedFrom.This method inspects the type of
fromand then delegates to thesubstream()variant corresponding to whetherfromis really aRoot,SetJoin,MapJoin, etc. You can use this method when you don't have more specific type information aboutfrom.- Type Parameters:
X- source typeY- target type- Parameters:
from- correlated join object for subquery- Returns:
- new subquery search stream
- Throws:
IllegalArgumentException- ifjoinis null- See Also:
-
substream
public <X,E> FromStream<E,jakarta.persistence.criteria.CollectionJoin<X, substreamE>> (jakarta.persistence.criteria.CollectionJoin<X, E> join) Create aSearchStreamfor use as a subquery, using the specified join.- Type Parameters:
X- join origin typeE- collection element type- Parameters:
join- correlated join object for subquery- Returns:
- new subquery search stream
- Throws:
IllegalArgumentException- ifjoinis null- See Also:
-
substream
public <X,E> FromStream<E,jakarta.persistence.criteria.ListJoin<X, substreamE>> (jakarta.persistence.criteria.ListJoin<X, E> join) Create aSearchStreamfor use as a subquery, using the specified join.- Type Parameters:
X- join origin typeE- list element type- Parameters:
join- correlated join object for subquery- Returns:
- new subquery search stream
- Throws:
IllegalArgumentException- ifjoinis null- See Also:
-
substream
public <X,K, FromStream<V,V> jakarta.persistence.criteria.MapJoin<X, substreamK, V>> (jakarta.persistence.criteria.MapJoin<X, K, V> join) Create aSearchStreamfor use as a subquery, using the specified join.- Type Parameters:
X- join origin typeK- map key typeV- map value type- Parameters:
join- correlated join object for subquery- Returns:
- new subquery search stream
- Throws:
IllegalArgumentException- ifjoinis null- See Also:
-
substream
public <X,E> FromStream<E,jakarta.persistence.criteria.SetJoin<X, substreamE>> (jakarta.persistence.criteria.SetJoin<X, E> join) Create aSearchStreamfor use as a subquery, using the specified join.- Type Parameters:
X- join origin typeE- set element type- Parameters:
join- correlated join object for subquery- Returns:
- new subquery search stream
- Throws:
IllegalArgumentException- ifjoinis null- See Also:
-
substream
public <X,E> FromStream<E,jakarta.persistence.criteria.Join<X, substreamE>> (jakarta.persistence.criteria.Join<X, E> join) Create aSearchStreamfor use as a subquery, using the specified join.- Type Parameters:
X- join origin typeE- collection element type- Parameters:
join- correlated join object for subquery- Returns:
- new subquery search stream
- Throws:
IllegalArgumentException- ifjoinis null- See Also:
-
deleteStream
Create aDeleteStreamfor bulk delete queries.- Type Parameters:
X- stream target type- Parameters:
type- stream target type- Returns:
- new bulk delete stream
- Throws:
IllegalArgumentException- iftypeis null
-
updateStream
Create aUpdateStreamfor bulk update queries.- Type Parameters:
X- stream target type- Parameters:
type- stream target type- Returns:
- new bulk update stream
- Throws:
IllegalArgumentException- iftypeis null
-
currentQuery
public jakarta.persistence.criteria.CommonAbstractCriteria currentQuery()Access the current Criteria API query under construction.This method provides a way to access the current
CriteriaQuery,CriteriaUpdate,CriteriaDelete, orSubquerycurrently being constructed.This is useful (for example) when implementing a
QueryStream.filter(Function)function using the traditional JPA Criteria API and you need to create aSubquery:List<String> names = qb.stream(Teacher.class) .filter(teacher -> { Subquery<Student> subquery = qb.currentQuery().subquery(Student.class); // configure Student subquery... return qb.exists(subquery); }) .map(Teacher_.name) .getResultList(); // note: the query is actually constructed hereThis method does not work outside of the context of a query being constructed.
In the case of nested substream(s), then the inner-most query is returned:
List<String> names = qb.stream(Teacher.class) .filter(teacher -> { // here qb.currentQuery() would return CriteriaQuery<Teacher> return qb.substream(teacher) .map(Teacher_.students) .filter(student -> { // here qb.currentQuery() returns CriteriaQuery<Student> Subquery<Test> subquery = qb.currentQuery().subquery(Test.class); // configure Test subquery... return qb.exists(subquery); }) .exists(); }) .map(Teacher_.name) .getResultList(); // here qb.currentQuery() will throw IllegalStateException qb.currentQuery(); // this will throw IllegalStateExceptionThe returned query object should not be modified.
- Returns:
- the current Criteria API query under construction
- Throws:
IllegalStateException- if invoked outside of Criteria API query construction
-
bindParam
Register a parameter binding with the currentQuerythat is under construction.This method addresses an inconvenience in the JPA Criteria API, which is that parameters are (a) used (i.e., within some Criteria API expression) and (b) bound (i.e., assigned a value) at two separate stages of query construction: parameters are used in the context of building a Criteria API
Predicate, but the value of the parameter can only be bound once the overallQueryhas been constructed. Often these two steps are implemented at different places in the code.This method allows the value of the parameter to be bound at the same time it is used. It simply remembers the parameter value until later when the
Queryis created and the value can then be actually assigned. However, this method only works forQuerys created via QueryStream API query execution methods, e.g.,QueryStream.toQuery(),SearchStream.getResultList(),DeleteStream.delete(),SearchValue.value(), etc.This example shows how parameters would usually be handled:
// Create parameter and get parameterized value Date startDateCutoff = ...; Parameter<Date> startDateParam = qb.parameter(Date.class); // Build Query Query query = qb.stream(Employee.class) .filter(e -> qb.greaterThan(e.get(Employee_.startDate), startDateParam)) // parameter used here .map(Employee_.name) .toQuery(); // Bind parameter value query.setParameter(paramRef.get(), startDateCutoff, TemporalType.DATE); // parameter bound here // Execute query return query.getResultStream();This example, which is functionally equivalent to the above, shows howbindParam()allows performing all of the parameter handling in one place:return qb.stream(Employee.class) .filter(e -> { Date startDateCutoff = ...; Parameter<Date> startDateParam = qb.parameter(Date.class); qb.bindParam(new DateParamBinding(startDateParam, startDateCutoff, TemporalType.DATE)); return qb.greaterThan(e.get(Employee_.startDate), param); }) .map(Employee_.name) .getResultStream(); // note: the Query is actually constructed hereIf this method is invoked outside of the context of
Queryconstruction, anIllegalStateExceptionis thrown.- Parameters:
binding- parameter binding- Throws:
IllegalStateException- if invoked outside ofQueryStream.toQuery()or other query execution methodIllegalArgumentException- ifbindingis null
-