- All Implemented Interfaces:
st.orm.core.template.impl.Subqueryable
QueryBuilderImpl relies on preview features of the Java platform:
QueryBuilderImplrefers to one or more preview APIs:StringTemplate.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbuild()Builds the query based on the current state of the query builder.Adds a cross join to the query.crossJoin(StringTemplatePREVIEW template) Adds a cross join to the query.QueryBuilder<T, R, ID> distinct()Marks the current query as a distinct query.QueryBuilder<T, R, ID> Resolves the references at the specified paths as part of this query.QueryBuilder<T, R, ID> forLock(StringTemplatePREVIEW template) Locks the selected rows using a custom lock mode.QueryBuilder<T, R, ID> forShare()Locks the selected rows for reading.QueryBuilder<T, R, ID> Locks the selected rows for reading.Executes the query and returns an optional result.longDelegates to the core builder, which executes a dedicated count query derived from the builder's shape.<V extends Data>
SequencedMap<V, List<R>> getResultGroupedBy(TypedMetamodel<T, V, V> path) Executes the query and returns the results grouped by the record reached viapath, typically the parent entity of a foreign key.<V extends Data>
SequencedMap<Ref<V>, List<R>> getResultGroupedByRef(Metamodel<T, V> path) Executes the query and returns the results grouped by a lightweight ref to the record reached viapath, typically the parent entity of a foreign key.Eager terminals delegate to the core builder, which executes them without the fetch-size hint, avoiding transaction wrapping for eagerly consumed results.Executes the query and returns a stream of results.Executes the query and returns a single result.st.orm.core.template.TemplateStringQueryBuilder<T, R, ID> groupBy(StringTemplatePREVIEW template) Adds a GROUP BY clause to the query using a string template.protected booleanReturnstrueif any ORDER BY columns have been added to this query builder.QueryBuilder<T, R, ID> having(StringTemplatePREVIEW template) Adds a HAVING clause to the query using the specified expression.QueryBuilder<T, R, ID> havingExists(QueryBuilder<?, ?, ?> subquery) Adds a HAVING clause that keeps the groups for which the specified subquery returns at least one row.QueryBuilder<T, R, ID> havingNotExists(QueryBuilder<?, ?, ?> subquery) Adds a HAVING clause that keeps the groups for which the specified subquery returns no rows.QueryBuilder<T, R, ID> Adds an optimizer hint to the statement this builder builds.Adds an inner join to the query.JoinBuilder<T, R, ID> innerJoin(StringTemplatePREVIEW template, String alias) Adds an inner join to the query.Adds a join of the specified type to the query.JoinBuilder<T, R, ID> join(JoinType type, StringTemplatePREVIEW template, String alias) Adds a join of the specified type to the query using a template.JoinBuilder<T, R, ID> join(JoinType type, QueryBuilder<?, ?, ?> subquery, String alias) Adds a join of the specified type to the query using a subquery.Adds a left join to the query.JoinBuilder<T, R, ID> leftJoin(StringTemplatePREVIEW template, String alias) Adds a left join to the query.QueryBuilder<T, R, ID> limit(int limit) Adds a LIMIT clause to the query.<X extends Data>
QueryBuilder<X, R, ID> Returns a query builder rooted at the specified type.QueryBuilder<T, R, ID> offset(int offset) Adds an OFFSET clause to the query.QueryBuilder<T, R, ID> orderBy(StringTemplatePREVIEW template) Adds an ORDER BY clause to the query using a string template.Adds a right join to the query.JoinBuilder<T, R, ID> rightJoin(StringTemplatePREVIEW template, String alias) Adds a right join to the query.scroll(Scrollable<T> scrollable) Executes a scroll request and returns aWindow: the results in the request's sort order, the flags that say whether rows exist after and before the window, and the tokens that continue from it.<X> QueryBuilder<T, R, X> Returns a typed query builder for the specified primary key type.QueryBuilder<T, R, ID> unsafe()Returns a query builder that allows UPDATE and DELETE queries without a WHERE clause.QueryBuilder<T, R, ID> where(Function<WhereBuilder<T, R, ID>, PredicateBuilder<T, ?, ?>> predicate) Adds a WHERE clause to the query using aWhereBuilder.widen()Widens the query as a join does, without joining.windows(int size) Executes the query in windows ofsizerows ordered by the primary key, each window one closed statement.windows(Scrollable<T> scrollable) Executes the query in windows described by the given scroll request, each window one closed statement.Methods inherited from class st.orm.template.QueryBuilder
executeUpdate, fetch, groupBy, having, orderBy, orderByDescending, orderByDescending, orderByDescending, page, page, page, prepare, slice, slice, where, where, where, where, where, where, where, where, where, where, whereExists, whereId, whereNotExists, whereRef, whereRef
-
Constructor Details
-
QueryBuilderImpl
-
-
Method Details
-
typedId
Returns a typed query builder for the specified primary key type.- Specified by:
typedIdin classQueryBuilder<T extends Data,R, ID> - Type Parameters:
X- the type of the primary key.- Parameters:
pkType- the primary key type.- Returns:
- the typed query builder.
- Throws:
PersistenceException- if the pk type is not valid.- Since:
- 1.14
-
narrow
Returns a query builder rooted at the specified type. -
widen
Widens the query as a join does, without joining. -
unsafe
Returns a query builder that allows UPDATE and DELETE queries without a WHERE clause.By default, Storm rejects UPDATE and DELETE queries that lack a WHERE clause, throwing a
PersistenceException. Call this method to disable that check when you intentionally want to affect all rows in the table. -
distinct
Marks the current query as a distinct query. -
fetch
Resolves the references at the specified paths as part of this query. -
orderBy
Adds an ORDER BY clause to the query using a string template. Multiple calls to this method append additional columns to the ORDER BY clause. -
groupBy
Adds a GROUP BY clause to the query using a string template. Multiple calls to this method append additional columns to the GROUP BY clause. -
having
Adds a HAVING clause to the query using the specified expression. Multiple calls to this method are combined using AND. -
havingExists
Adds a HAVING clause that keeps the groups for which the specified subquery returns at least one row.- Specified by:
havingExistsin classQueryBuilder<T extends Data,R, ID> - Parameters:
subquery- the subquery to test for existence.- Returns:
- the query builder.
- Since:
- 1.13
-
havingNotExists
Adds a HAVING clause that keeps the groups for which the specified subquery returns no rows.- Specified by:
havingNotExistsin classQueryBuilder<T extends Data,R, ID> - Parameters:
subquery- the subquery to test for absence.- Returns:
- the query builder.
- Since:
- 1.13
-
hasOrderBy
protected boolean hasOrderBy()Returnstrueif any ORDER BY columns have been added to this query builder.- Specified by:
hasOrderByin classQueryBuilder<T extends Data,R, ID> - Returns:
trueif ORDER BY columns are present,falseotherwise.- Since:
- 1.9
-
forUpdate
Locks the selected rows for reading.- Specified by:
forUpdatein classQueryBuilder<T extends Data,R, ID> - Returns:
- the query builder.
- Throws:
PersistenceException- if the database does not support the specified lock mode, or if the lock mode is not supported for the current query.- Since:
- 1.2
-
forLock
Locks the selected rows using a custom lock mode.Note: This method results in non-portable code, as the lock mode is specific to the underlying database.
- Specified by:
forLockin classQueryBuilder<T extends Data,R, ID> - Returns:
- the query builder.
- Throws:
PersistenceException- if the lock mode is not supported for the current query.- Since:
- 1.2
-
hint
Adds an optimizer hint to the statement this builder builds. -
build
Builds the query based on the current state of the query builder. -
getResultStream
Executes the query and returns a stream of results.The resulting stream is lazily loaded, meaning that the records are only retrieved from the database as they are consumed by the stream. This approach is efficient and minimizes the memory footprint, especially when dealing with large volumes of records.
Note: Calling this method does trigger the execution of the underlying query, so it should only be invoked when the query is intended to run. Since the stream holds resources open while in use, it must be closed after usage to prevent resource leaks.
- Specified by:
getResultStreamin classQueryBuilder<T extends Data,R, ID> - Returns:
- a stream of results.
- Throws:
PersistenceException- if the query operation fails due to underlying database issues, such as connectivity.
-
getResultCount
public long getResultCount()Delegates to the core builder, which executes a dedicated count query derived from the builder's shape.- Overrides:
getResultCountin classQueryBuilder<T extends Data,R, ID> - Returns:
- the total number of results of this query as a long value.
-
getResultList
Eager terminals delegate to the core builder, which executes them without the fetch-size hint, avoiding transaction wrapping for eagerly consumed results.- Specified by:
getResultListin classQueryBuilder<T extends Data,R, ID> - Returns:
- the list of results.
-
getSingleResult
Description copied from class:QueryBuilderExecutes the query and returns a single result.- Specified by:
getSingleResultin classQueryBuilder<T extends Data,R, ID> - Returns:
- the single result.
-
getOptionalResult
Description copied from class:QueryBuilderExecutes the query and returns an optional result.- Specified by:
getOptionalResultin classQueryBuilder<T extends Data,R, ID> - Returns:
- the optional result.
-
getResultGroupedBy
Description copied from class:QueryBuilderExecutes the query and returns the results grouped by the record reached viapath, typically the parent entity of a foreign key. The SQL is not affected by the grouping; the same select is executed and the results are grouped during hydration.The returned map and its lists are unmodifiable and insertion-ordered: groups appear in the order their first result is encountered, and results appear in encounter order within each group. Use
orderBy()to control both. Duplicate entities within a result set are guaranteed to share the same instance as long as earlier occurrences remain strongly reachable, and the grouping retains every result and group key while the result set is consumed; each result's reference to its group key is therefore the map key itself.This method requires an entity query: the result type must be the table type
Tso that the path can be resolved against the results. The path must also resolve to a non-null record for every result; paths over nullable foreign keys must be narrowed with awhere()clause first.The signature requires a path whose component type equals its field type, which is how the generated metamodels type eagerly fetched fields. Paths over
Reffields are typedTypedMetamodel<T, V, Ref<V>>and therefore do not compile; useQueryBuilder.getResultGroupedByRef(Metamodel)for those.- Specified by:
getResultGroupedByin classQueryBuilder<T extends Data,R, ID> - Type Parameters:
V- the type of the record to group by.- Parameters:
path- the metamodel path from the table type to the record to group by, for examplePet_.owner.- Returns:
- the results grouped by the record reached via
path, in encounter order.
-
getResultGroupedByRef
Description copied from class:QueryBuilderExecutes the query and returns the results grouped by a lightweight ref to the record reached viapath, typically the parent entity of a foreign key. The SQL is not affected by the grouping; the same select is executed and the results are grouped during hydration.This is the ref-based variant of
QueryBuilder.getResultGroupedBy(TypedMetamodel): the map keys areRefinstances, which are compared by primary key, keeping map lookups constant-cost regardless of the size of the group record.The behavior of the keys follows how the foreign key is declared on the record:
- Entity field (for example
@FK Owner owner): the referenced record is fetched eagerly, as part of the query's auto-joined graph, and is materialized with each result. The keys are loaded refs wrapping that record:Ref.getOrNull()returns it directly, without touching the database. - Ref field (for example
@FK Ref<Pet> pet): the referenced record is fetched lazily; the query reads only the foreign key column, without joining or fetching the referenced table. The keys are the unloaded refs produced by the query, carrying just the primary key. When the records are needed, fetch them afterwards in a single query withfindAllByRef(map.keySet()).
The returned map and its lists are unmodifiable and insertion-ordered: groups appear in the order their first result is encountered, and results appear in encounter order within each group. Use
orderBy()to control both.This method requires an entity query: the result type must be the table type
Tso that the path can be resolved against the results. The path must also resolve to a non-null value for every result; paths over nullable foreign keys must be narrowed with awhere()clause first.- Specified by:
getResultGroupedByRefin classQueryBuilder<T extends Data,R, ID> - Type Parameters:
V- the type of the record to group by.- Parameters:
path- the metamodel path from the table type to the record to group by, for examplePet_.owner.- Returns:
- the results grouped by a ref to the record reached via
path, in encounter order.
- Entity field (for example
-
crossJoin
Adds a cross join to the query. -
innerJoin
Adds an inner join to the query. -
leftJoin
Adds a left join to the query. -
rightJoin
Adds a right join to the query. -
join
Adds a join of the specified type to the query. -
crossJoin
Adds a cross join to the query. -
innerJoin
Adds an inner join to the query. -
leftJoin
Adds a left join to the query. -
rightJoin
Adds a right join to the query. -
join
Adds a join of the specified type to the query using a template. -
join
Adds a join of the specified type to the query using a subquery. -
where
Adds a WHERE clause to the query using aWhereBuilder. -
limit
Adds a LIMIT clause to the query. -
offset
Adds an OFFSET clause to the query. -
scroll
Description copied from class:QueryBuilderExecutes a scroll request and returns aWindow: the results in the request's sort order, the flags that say whether rows exist after and before the window, and the tokens that continue from it.The request owns the ordering, so the query must not carry an ORDER BY of its own. The sort fields and the key are read from each row alongside the result, so the tokens are there for every result type: the entity, a projection, a ref or a custom select type. A window reached through
Window.previous()comes back in the same sort order as every other window. -
windows
Description copied from class:QueryBuilderExecutes the query in windows ofsizerows ordered by the primary key, each window one closed statement.Where
QueryBuilder.getResultStream()holds one open statement on the connection for as long as the stream is consumed, a window is fetched by a statement that has returned and closed before the window is handed to the caller. Between windows the connection is free, so the loop over a window may query, fetch references and write, inside a transaction or with a transaction per window. The stream carries no database resource and needs no closing. Each window is its own statement: it runs the query's WHERE clause again from the cursor position, and under READ COMMITTED it sees rows committed since the previous window.Windows are keyset windows over the primary key, so the query must not carry an ORDER BY of its own, and the result type must be the entity type the key belongs to:
selectRef()and custom select types are refused. A compound primary key is refused too; passQueryBuilder.windows(Scrollable)a single-column unique key instead. The rows come in ascending key order; passScrollable.of(key, size).descending()toQueryBuilder.windows(Scrollable)for descending order.users.select().where(User_.city, EQUALS, city).windows(1000).forEach(window -> users.update(window.content().stream() .map(user -> new User(user.id(), user.email().toLowerCase(), user.birthDate(), user.street(), user.postalCode(), user.city())) .toList()));- Specified by:
windowsin classQueryBuilder<T extends Data,R, ID> - Parameters:
size- the maximum number of rows per window (must be positive).- Returns:
- a stream of windows; each window's
Window.next()resumes the iteration after that window.
-
windows
Description copied from class:QueryBuilderExecutes the query in windows described by the given scroll request, each window one closed statement.This is the form of
QueryBuilder.windows(int)that chooses the key, the sort fields, the directions and the starting position:Scrollable.of(key, size)iterates from the start, aWindow.next()token orScrollable.from(String)resumes after an earlier window, and.descending()iterates in descending key order. The same key rules asQueryBuilder.scroll(Scrollable)apply.- Specified by:
windowsin classQueryBuilder<T extends Data,R, ID> - Parameters:
scrollable- the scroll request describing key, sort, size, direction and starting position.- Returns:
- a stream of windows; each window's
Window.next()resumes the iteration after that window.
-
getSubquery
public st.orm.core.template.TemplateString getSubquery()- Specified by:
getSubqueryin interfacest.orm.core.template.impl.Subqueryable
-
QueryBuilderImplwhen preview features are enabled.