- Type Parameters:
T- the entity bean type (normal entity bean type e.g. Customer)R- the specific root query bean type (e.g. QCustomer)
- All Implemented Interfaces:
io.ebean.QueryBuilder<R,,T> io.ebean.QueryBuilderProjection<R,,T> IQueryBean<T,R>
For each entity bean querybean-generator generates a query bean that extends TQRootBean.
Example - usage of QCustomer
Date fiveDaysAgo = ...
List<Customer> customers =
new QCustomer()
.name.ilike("rob")
.status.equalTo(Customer.Status.GOOD)
.registered.after(fiveDaysAgo)
.contacts.email.endsWith("@foo.com")
.orderBy()
.name.asc()
.registered.desc()
.findList();
Resulting SQL where
where lower(t0.name) like ? and t0.status = ? and t0.registered > ? and u1.email like ?
order by t0.name, t0.registered desc;
--bind(rob,GOOD,Mon Jul 27 12:05:37 NZST 2015,%@foo.com)
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedQueryBean(boolean aliasDummy) Construct for using as an 'Alias' to use the properties as known string values for select() and fetch().protectedConstruct for FilterManyprotectedConstruct using a query.protectedConstruct using the type of bean to query on and the default database.protectedConstruct using the type of bean to query on and a given database. -
Method Summary
Modifier and TypeMethodDescriptionfinal Radd(io.ebean.Expression expression) Add an expression to the WHERE or HAVING clause.final Rfinal Rfinal RalsoIf(BooleanSupplier predicate, Consumer<R> apply) final Rand()Begin a list of expressions added by 'AND'.final Rapply(io.ebean.FetchPath pathProperties) final <D> io.ebean.DtoQuery<D> final Rfinal io.ebean.UpdateQuery<T> asUpdate()io.ebean.FetchGroup<T> Return the fetch group.final intdelete()final RdistinctOn(TQProperty<R, ?>... properties) Set DISTINCT ON properties.distinctOn(String distinctOn) final RendAnd()End AND junction - synonym for endJunction().final REnd a list of expressions added by 'OR'.final RendNot()End NOT junction - synonym for endJunction().final RendOr()End OR junction - synonym for endJunction().final booleanexists()final Rexists(io.ebean.Query<?> subQuery) Add EXISTS sub-query predicate.final REXISTS using a SQL SubQuery.final Rfinal Rfinal Rfinal Rfinal RfetchCache(String path) final RfetchCache(String path, String properties) final RfetchQuery(String path) final RfetchQuery(String path, String properties) final intfinal voidfinal voidfinal voidfindEachWhile(Predicate<T> consumer) final io.ebean.FutureRowCount<T> final io.ebean.FutureIds<T> final io.ebean.FutureList<T> final <K> io.ebean.FutureMap<K, T> final <A> List<A> findIds()final io.ebean.QueryIterator<T> findList()findMap()final @Nullable TfindOne()final io.ebean.PagedList<T> findSet()final <A> @Nullable Afinal <A> List<A> final <A> Optional<A> final <A> Set<A> findVersionsBetween(Timestamp start, Timestamp end) final Rfinal Rfinal Rfinal io.ebean.ExpressionList<T> Return the expression list that has been built for this query.final Stringfinal Rhaving()Start adding expressions to the having clause when using @Aggregation properties.final io.ebean.ExpressionList<T> Return the underlying having clause to typically when using dynamic aggregation formula.final RinTuples(io.ebean.InTuples inTuples) In expression using multiple columns.final Rnot()Begin a list of expressions added by NOT.final RnotExists(io.ebean.Query<?> subQuery) Add NOT EXISTS sub-query predicate.final RNot EXISTS using a SQL SubQuery.final Ror()Begin a list of expressions added by 'OR'.final Rorder()Deprecated, for removal: This API element is subject to removal in a future version.final RDeprecated, for removal: This API element is subject to removal in a future version.final RorderBy()Marker that can be used to indicate that the order by clause is defined after this.final RorderById(boolean orderById) protected final io.ebean.ExpressionList<T> Return the current expression list that expressions should be added to.io.ebean.Query<T> query()Return the underlying query.final RAdd raw expression with no parameters.final RAdd raw expression with a single parameter.final RAdd raw expression with an array of parameters.final RrawOrEmpty(String raw, Collection<?> values) Only add the raw expression if the values is not null or empty.final Rselect(io.ebean.Query.Property<?>... properties) Specify the properties to be loaded on the 'main' root level entity bean also allowing for functions to be used likeStdOperators.max(Query.Property).final Rselect(TQProperty<R, ?>... properties) Specify the properties to be loaded on the 'main' root level entity bean.final Rfinal RsetAutoTune(boolean autoTune) final RsetBaseTable(String baseTable) final RsetBeanCacheMode(io.ebean.CacheMode beanCacheMode) final RsetBufferFetchSizeHint(int fetchSize) setCountDistinct(io.ebean.CountDistinctOrder orderBy) final RsetDisableLazyLoading(boolean disableLazyLoading) final RsetDistinct(boolean distinct) final RsetFirstRow(int firstRow) final Rfinal RSet the Id value to query.final RSet a list of Id values to match.final RsetIdIn(Collection<?> ids) Set a collection of Id values to match.final Rfinal Rfinal RsetLazyLoadBatchSize(int lazyLoadBatchSize) final Rfinal RsetMaxRows(int maxRows) setOrderBy(io.ebean.OrderBy<T> orderBy) setPaging(io.ebean.Paging paging) final RsetPersistenceContextScope(io.ebean.PersistenceContextScope scope) final RsetProfileLocation(io.ebean.ProfileLocation profileLocation) Set the profile location of this query.final RsetRawSql(io.ebean.RawSql rawSql) protected voidThe enhancement will no longer use this method.final RsetTimeout(int secs) setUnmodifiable(boolean unmodifiable) final RsetUseQueryCache(io.ebean.CacheMode cacheMode) final RusingConnection(Connection connection) usingDatabase(io.ebean.Database database) final Rfinal RusingTransaction(io.ebean.Transaction transaction) validate()final Rwhere()Add expression after this to the WHERE expression list.final RwithLock(io.ebean.Query.LockType lockType) final RwithLock(io.ebean.Query.LockType lockType, io.ebean.Query.LockWait lockWait) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.ebean.QueryBuilder
copy, setUseCache, setUseQueryCache
-
Constructor Details
-
QueryBean
Construct using the type of bean to query on and the default database. -
QueryBean
Construct using the type of bean to query on and a given database. -
QueryBean
Construct using a query. -
QueryBean
protected QueryBean(boolean aliasDummy) Construct for using as an 'Alias' to use the properties as known string values for select() and fetch(). -
QueryBean
Construct for FilterMany
-
-
Method Details
-
setRoot
The enhancement will no longer use this method. Will be removed once the new IntelliJ plugin is released. -
buildFetchGroup
Description copied from interface:IQueryBeanReturn the fetch group.- Specified by:
buildFetchGroupin interfaceIQueryBean<T,R extends QueryBean<T, R>>
-
query
Description copied from interface:IQueryBeanReturn the underlying query.Generally it is not expected that you will need to do this but typically use the find methods available on this 'root query bean' instance like findList().
-
distinctOn
-
select
-
select
-
distinctOn
Description copied from interface:IQueryBeanSet DISTINCT ON properties. This is a Postgres only SQL feature.- Specified by:
distinctOnin interfaceIQueryBean<T,R extends QueryBean<T, R>> - Parameters:
properties- The properties to include in the DISTINCT ON clause.
-
select
Description copied from interface:IQueryBeanSpecify the properties to be loaded on the 'main' root level entity bean.The resulting entities with be "partially loaded" aka partial objects.
Alternatively we can use a
QueryBuilderProjection.select(FetchGroup)to specify all properties to load on all parts of the graph.// alias for the customer properties in select() QCustomer cust = QCustomer.alias(); // alias for the contact properties in contacts.fetch() QContact contact = QContact.alias(); List<Customer> customers = new QCustomer() // specify the parts of the graph we want to load .select(cust.id, cust.name) .contacts.fetch(contact.firstName, contact.lastName, contact.email) // predicates .id.gt(1) .findList(); -
select
Description copied from interface:IQueryBeanSpecify the properties to be loaded on the 'main' root level entity bean also allowing for functions to be used likeStdOperators.max(Query.Property). -
fetch
-
fetchQuery
-
fetchCache
-
fetchQuery
-
fetchCache
-
fetch
-
fetch
-
fetch
-
fetchLazy
-
fetchLazy
-
apply
-
also
-
alsoIf
-
asOf
-
setIncludeSoftDeletes
-
add
Description copied from interface:IQueryBeanAdd an expression to the WHERE or HAVING clause. -
alias
-
setPaging
-
setMaxRows
-
setFirstRow
-
setAllowLoadErrors
-
setAutoTune
-
setBufferFetchSizeHint
-
setDistinct
-
setBaseTable
-
withLock
-
withLock
-
exists
Description copied from interface:IQueryBeanAdd EXISTS sub-query predicate. -
notExists
Description copied from interface:IQueryBeanAdd NOT EXISTS sub-query predicate. -
exists
Description copied from interface:IQueryBeanEXISTS using a SQL SubQuery. -
notExists
Description copied from interface:IQueryBeanNot EXISTS using a SQL SubQuery. -
forUpdate
-
forUpdateNoWait
-
forUpdateSkipLocked
-
asUpdate
-
asDto
-
setId
Description copied from interface:IQueryBeanSet the Id value to query. This is used with findOne().You can use this to have further control over the query. For example adding fetch joins.
Order order = new QOrder() .setId(1) .fetch("details") .findOne(); // the order details were eagerly fetched List<OrderDetail> details = order.getDetails(); -
setIdIn
Description copied from interface:IQueryBeanSet a list of Id values to match.List<Order> orders = new QOrder() .setIdIn(42, 43, 44) .findList(); -
setIdIn
Description copied from interface:IQueryBeanSet a collection of Id values to match.Collection<?> ids = ... List<Order> orders = new QOrder() .setIdIn(ids) .findList(); -
setLabel
-
setHint
-
setProfileLocation
Description copied from interface:IQueryBeanSet the profile location of this query. This is used to relate query execution metrics back to a location like a specific line of code.- Specified by:
setProfileLocationin interfaceIQueryBean<T,R extends QueryBean<T, R>>
-
setLazyLoadBatchSize
-
setMapKey
-
setPersistenceContextScope
-
setCountDistinct
-
setRawSql
-
setUnmodifiable
-
setBeanCacheMode
-
setDisableLazyLoading
-
setUseQueryCache
-
setTimeout
-
validate
-
raw
Description copied from interface:IQueryBeanAdd raw expression with no parameters.When properties in the clause are fully qualified as table-column names then they are not translated. logical property name names (not fully qualified) will still be translated to their physical name.
raw("orderQty < shipQty")Subquery example:
.raw("t0.customer_id in (select customer_id from customer_group where group_id = any(?::uuid[]))", groupIds) -
raw
Description copied from interface:IQueryBeanAdd raw expression with an array of parameters.The raw expression should contain the same number of ? as there are parameters.
When properties in the clause are fully qualified as table-column names then they are not translated. logical property name names (not fully qualified) will still be translated to their physical name.
-
rawOrEmpty
Description copied from interface:IQueryBeanOnly add the raw expression if the values is not null or empty.This is a pure convenience expression to make it nicer to deal with the pattern where we use raw() expression with a subquery and only want to add the subquery predicate when the collection of values is not empty.
Without inOrEmpty()
QCustomer query = new QCustomer() // add some predicates .status.equalTo(Status.NEW); // common pattern - we can use rawOrEmpty() instead if (orderIds != null && !orderIds.isEmpty()) { query.raw("t0.customer_id in (select o.customer_id from orders o where o.id in (?1))", orderIds); } query.findList();Using rawOrEmpty()
Note that in the example below we use the?1bind parameter to get "parameter expansion" for each element in the collection.new QCustomer() .status.equalTo(Status.NEW) // only add the expression if orderIds is not empty .rawOrEmpty("t0.customer_id in (select o.customer_id from orders o where o.id in (?1))", orderIds); .findList();Postgres ANY
With Postgres we would often use the SQLANYexpression and array parameter binding rather thanIN.new QCustomer() .status.equalTo(Status.NEW) .rawOrEmpty("t0.customer_id in (select o.customer_id from orders o where o.id = any(?))", orderIds); .findList();Note that we need to cast the Postgres array for UUID types like:
" ... = any(?::uuid[])"- Specified by:
rawOrEmptyin interfaceIQueryBean<T,R extends QueryBean<T, R>> - Parameters:
raw- The raw expression that is typically a subqueryvalues- The values which is typically a list or set of id values.
-
raw
Description copied from interface:IQueryBeanAdd raw expression with a single parameter.The raw expression should contain a single ? at the location of the parameter.
When properties in the clause are fully qualified as table-column names then they are not translated. logical property name names (not fully qualified) will still be translated to their physical name.
Example:
// use a database function raw("add_days(orderDate, 10) < ?", someDate)Subquery example:
.raw("t0.customer_id in (select customer_id from customer_group where group_id = any(?::uuid[]))", groupIds) -
inTuples
Description copied from interface:IQueryBeanIn expression using multiple columns. -
orderBy
Description copied from interface:IQueryBeanMarker that can be used to indicate that the order by clause is defined after this.Example: order by customer name, order date
List<Order> orders = new QOrder() .customer.name.ilike("rob") .orderBy() .customer.name.asc() .orderDate.asc() .findList(); -
order
Deprecated, for removal: This API element is subject to removal in a future version. -
orderBy
-
setOrderBy
-
orderById
-
order
Deprecated, for removal: This API element is subject to removal in a future version. -
or
Description copied from interface:IQueryBeanBegin a list of expressions added by 'OR'.Use endOr() or endJunction() to stop added to OR and 'pop' to the parent expression list.
Example
This example uses an 'OR' expression list with an inner 'AND' expression list.
List<Customer> customers = new QCustomer() .status.equalTo(Customer.Status.GOOD) .or() .id.greaterThan(1000) .and() .name.startsWith("super") .registered.after(fiveDaysAgo) .endAnd() .endOr() .orderBy().id.desc() .findList();Resulting SQL where clause
sql where t0.status = ? and (t0.id > ? or (t0.name like ? and t0.registered > ? ) ) order by t0.id desc; --bind(GOOD,1000,super%,Wed Jul 22 00:00:00 NZST 2015) -
and
Description copied from interface:IQueryBeanBegin a list of expressions added by 'AND'.Use endAnd() or endJunction() to stop added to AND and 'pop' to the parent expression list.
Note that typically the AND expression is only used inside an outer 'OR' expression. This is because the top level expression list defaults to an 'AND' expression list.
Example
This example uses an 'OR' expression list with an inner 'AND' expression list.
List<Customer> customers = new QCustomer() .status.equalTo(Customer.Status.GOOD) .or() // OUTER 'OR' .id.greaterThan(1000) .and() // NESTED 'AND' expression list .name.startsWith("super") .registered.after(fiveDaysAgo) .endAnd() .endOr() .orderBy().id.desc() .findList();Resulting SQL where clause
sql where t0.status = ? and (t0.id > ? or (t0.name like ? and t0.registered > ? ) ) order by t0.id desc; --bind(GOOD,1000,super%,Wed Jul 22 00:00:00 NZST 2015) -
not
Description copied from interface:IQueryBeanBegin a list of expressions added by NOT.Use endNot() or endJunction() to stop added to NOT and 'pop' to the parent expression list.
-
endJunction
Description copied from interface:IQueryBeanEnd a list of expressions added by 'OR'.- Specified by:
endJunctionin interfaceIQueryBean<T,R extends QueryBean<T, R>>
-
endOr
Description copied from interface:IQueryBeanEnd OR junction - synonym for endJunction(). -
endAnd
Description copied from interface:IQueryBeanEnd AND junction - synonym for endJunction(). -
endNot
Description copied from interface:IQueryBeanEnd NOT junction - synonym for endJunction(). -
where
Description copied from interface:IQueryBeanAdd expression after this to the WHERE expression list.For queries against the normal database (not the doc store) this has no effect.
This is intended for use with Document Store / ElasticSearch where expressions can be put into either the "query" section or the "filter" section of the query. Full text expressions like MATCH are in the "query" section but many expression can be in either - expressions after the where() are put into the "filter" section which means that they don't add to the relevance and are also cache-able.
-
usingTransaction
-
usingConnection
-
usingDatabase
-
usingMaster
-
exists
public final boolean exists() -
findOne
-
findOneOrEmpty
-
findList
-
findStream
-
findSet
-
findIds
-
findMap
-
findIterate
-
findSingleAttributeList
-
findSingleAttributeSet
-
findSingleAttribute
public final <A> @Nullable A findSingleAttribute() -
findSingleAttributeOrEmpty
-
findEach
-
findEach
-
findEachWhile
-
findVersions
-
findVersionsBetween
-
findCount
public final int findCount() -
findFutureCount
-
findFutureIds
-
findFutureList
-
findFutureMap
-
findPagedList
-
delete
public final int delete() -
getGeneratedSql
-
getBeanType
-
getExpressionList
Description copied from interface:IQueryBeanReturn the expression list that has been built for this query.- Specified by:
getExpressionListin interfaceIQueryBean<T,R extends QueryBean<T, R>>
-
having
Description copied from interface:IQueryBeanStart adding expressions to the having clause when using @Aggregation properties.new QMachineUse() // where ... .date.inRange(fromDate, toDate) .having() .sumHours.greaterThan(1) .findList() // The sumHours property uses @Aggregation // e.g. @Aggregation("sum(hours)") -
havingClause
Description copied from interface:IQueryBeanReturn the underlying having clause to typically when using dynamic aggregation formula.Note that after this we no longer have the query bean so typically we use this right at the end of the query.
// sum(distanceKms) ... is a "dynamic formula" // so we use havingClause() for it like: List<MachineUse> machineUse = new QMachineUse() .select("machine, sum(fuelUsed), sum(distanceKms)") // where ... .date.greaterThan(LocalDate.now().minusDays(7)) .havingClause() .gt("sum(distanceKms)", 2) .findList();- Specified by:
havingClausein interfaceIQueryBean<T,R extends QueryBean<T, R>>
-
peekExprList
Return the current expression list that expressions should be added to.
-