Class Query<T>
- Type Parameters:
T- Entity class for which results are returned.
- All Implemented Interfaces:
Closeable,AutoCloseable
Use find() or related methods to fetch the latest results from the BoxStore.
Use property(Property) to only return values or an aggregate of a single Property.
Make sure to close() this query once done with it to reclaim resources immediately.
See the Queries documentation for details.
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this query and frees used resources.copy()Creates a copy of this for use in another thread.longcount()Returns the count of Objects matching the query.describe()For logging and testing, returns a string describing this query like "Query for entity Example with 4 conditions with properties prop1, prop2".For logging and testing, returns a string describing the conditions of this query like "(prop1 == A AND prop2 is null)".protected voidfinalize()Explicitly callclose()instead to avoid expensive finalization.find()Finds objects matching the query.find(long offset, long limit) Likefind(), but can skip and limit results.Finds the first object matching this query.longLikefindFirst(), but returns just the ID of the object.long[]findIds()Likefind(), but returns just the IDs of the objects.long[]findIds(long offset, long limit) LikefindIds(), but can skip and limit results.Finds IDs of objects matching the query associated to their query score (e.g.findIdsWithScores(long offset, long limit) LikefindIdsWithScores(), but can skip and limit results.findLazy()Finds the only object matching this query.longLikefindUnique(), but returns just the ID of the object.Finds objects matching the query associated to their query score (e.g.findWithScores(long offset, long limit) LikefindWithScores(), but can skip and limit results.voidforEach(QueryConsumer<T> consumer) Emits query results one by one to the given consumer (synchronously).Creates aPropertyQueryfor the given property.voidpublish()Publishes the current data to all subscribed @DataObservers.longremove()Removes (deletes) all Objects matching the querysetParameter(Property<?> property, boolean value) Sets a parameter previously given to theQueryBuilderto a new value.setParameter(Property<?> property, byte[] value) Sets a parameter previously given to theQueryBuilderto new values.setParameter(Property<?> property, double value) Sets a parameter previously given to theQueryBuilderto a new value.setParameter(Property<?> property, float[] value) Changes the parameter of the query condition forpropertyto a newvalue.setParameter(Property<?> property, int[] value) Changes the parameter of the query condition forpropertyto a newvalue.setParameter(Property<?> property, long value) Sets a parameter previously given to theQueryBuilderto a new value.setParameter(Property<?> property, long[] value) Changes the parameter of the query condition forpropertyto a newvalue.setParameter(Property<?> property, String value) Sets a parameter previously given to theQueryBuilderto a new value.setParameter(Property<?> property, String[] value) Changes the parameter of the query condition forpropertyto a newvalue.setParameter(Property<?> property, Date value) Sets a parameter previously given to theQueryBuilderto a new value.setParameter(String alias, boolean value) Changes the parameter of the query condition with the matchingaliasto a newvalue.setParameter(String alias, byte[] value) Changes the parameter of the query condition with the matchingaliasto a newvalue.setParameter(String alias, double value) Changes the parameter of the query condition with the matchingaliasto a newvalue.setParameter(String alias, float[] value) Changes the parameter of the query condition with the matchingaliasto a newvalue.setParameter(String alias, int[] value) Changes the parameter of the query condition with the matchingaliasto a newvalue.setParameter(String alias, long value) Changes the parameter of the query condition with the matchingaliasto a newvalue.setParameter(String alias, long[] value) Changes the parameter of the query condition with the matchingaliasto a newvalue.setParameter(String alias, String value) Changes the parameter of the query condition with the matchingaliasto a newvalue.setParameter(String alias, String[] value) Changes the parameter of the query condition with the matchingaliasto a newvalue.setParameter(String alias, Date value) Changes the parameter of the query condition with the matchingaliasto a newvalue.setParameters(Property<?> property, double value1, double value2) Sets a parameter previously given to theQueryBuilderto new values.setParameters(Property<?> property, int[] values) Deprecated.setParameters(Property<?> property, long[] values) Deprecated.UsesetParameter(Property, long[])instead.setParameters(Property<?> property, long value1, long value2) Sets a parameter previously given to theQueryBuilderto new values.setParameters(Property<?> property, String[] values) Deprecated.UsesetParameter(Property, String[])instead.setParameters(Property<?> property, String key, String value) Sets a parameter previously given to theQueryBuilderto new values.setParameters(String alias, double value1, double value2) Changes the parameters of the query condition with the matchingaliasto the new values.setParameters(String alias, int[] values) Deprecated.UsesetParameter(String, int[])instead.setParameters(String alias, long[] values) Deprecated.UsesetParameter(String, long[])instead.setParameters(String alias, long value1, long value2) Changes the parameters of the query condition with the matchingaliasto the new values.setParameters(String alias, String[] values) Deprecated.UsesetParameter(String, String[])instead.setParameters(String alias, String key, String value) Changes the parameters of the query condition with the matchingaliasto the new values.ADataObservercan be subscribed to data changes using the returned builder.subscribe(DataSubscriptionList dataSubscriptionList) Convenience forsubscribe()with a subsequent call toSubscriptionBuilder.dataSubscriptionList(DataSubscriptionList).
-
Method Details
-
finalize
Explicitly callclose()instead to avoid expensive finalization. -
close
public void close()Closes this query and frees used resources.If possible, call this always once done with this. Otherwise, will be called once this is finalized (e.g. garbage collected).
Calling any other methods of this afterwards will throw an
IllegalStateException.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
copy
-
findFirst
Finds the first object matching this query.Note: if no
QueryBuilder.order(io.objectbox.Property<T>)conditions are present, which object is the first one might be arbitrary (sometimes the one with the lowest ID, but never guaranteed to be).- Returns:
- The first object if there are matches.
nullif no object matches.
-
findUnique
Finds the only object matching this query.- Returns:
- The object if a single object matches.
nullif no object matches. ThrowsNonUniqueResultExceptionif there are multiple objects matching the query.
-
find
Finds objects matching the query.Note: if no
QueryBuilder.order(io.objectbox.Property<T>)conditions are present, the order is arbitrary (sometimes ordered by ID, but never guaranteed to).- Returns:
- A list of matching objects. An empty list if no object matches.
-
find
Likefind(), but can skip and limit results.Use to get a slice of the whole result, e.g. for "result paging".
- Parameters:
offset- If greater than 0, skips this many results.limit- If greater than 0, returns at most this many results.
-
findFirstId
public long findFirstId()LikefindFirst(), but returns just the ID of the object.This is more efficient as no object is created.
Ignores any
query filter.- Returns:
- The ID of the first matching object.
0if no object matches.
-
findUniqueId
public long findUniqueId()LikefindUnique(), but returns just the ID of the object.This is more efficient as no object is created.
Ignores any
query filter.- Returns:
- The ID of the object, if a single object matches.
0if no object matches. ThrowsNonUniqueResultExceptionif there are multiple objects matching the query.
-
findIds
@Nonnull public long[] findIds()Likefind(), but returns just the IDs of the objects.IDs can later be used to
Box.get(long)objects.This is very efficient as no objects are created.
Note: a filter set with
QueryBuilder.filter(QueryFilter)will be silently ignored!- Returns:
- An array of IDs of matching objects. An empty array if no objects match.
-
findIds
@Nonnull public long[] findIds(long offset, long limit) LikefindIds(), but can skip and limit results.Use to get a slice of the whole result, e.g. for "result paging".
Note: a filter set with
QueryBuilder.filter(QueryFilter)will be silently ignored!- Parameters:
offset- If greater than 0, skips this many results.limit- If greater than 0, returns at most this many results.
-
findLazy
-
findLazyCached
-
findIdsWithScores
LikefindIdsWithScores(), but can skip and limit results.Use to get a slice of the whole result, e.g. for "result paging".
- Parameters:
offset- If greater than 0, skips this many results.limit- If greater than 0, returns at most this many results.
-
findIdsWithScores
Finds IDs of objects matching the query associated to their query score (e.g. distance in NN search).This only works on objects with a property with an
HnswIndex.- Returns:
- A list of
IdWithScorethat wraps IDs of matching objects and their score, sorted by score in ascending order.
-
findWithScores
LikefindWithScores(), but can skip and limit results.Use to get a slice of the whole result, e.g. for "result paging".
- Parameters:
offset- If greater than 0, skips this many results.limit- If greater than 0, returns at most this many results.
-
findWithScores
Finds objects matching the query associated to their query score (e.g. distance in NN search).This only works on objects with a property with an
HnswIndex.- Returns:
- A list of
ObjectWithScorethat wraps matching objects and their score, sorted by score in ascending order.
-
property
Creates aPropertyQueryfor the given property.A
PropertyQueryuses the same conditions as this Query object, but returns only the value(s) of a single property (not an entity objects).- Parameters:
property- the property for which to return values
-
forEach
Emits query results one by one to the given consumer (synchronously). Once this method returns, the consumer will have received all result object). It "streams" each object from the database to the consumer, which is very memory efficient. Because this is run in a read transaction, the consumer gets a consistent view on the data. LikefindLazy(), this method can be used for a high amount of data.Note: because the consumer is called within a read transaction it may not write to the database.
-
count
public long count()Returns the count of Objects matching the query. -
setParameter
Sets a parameter previously given to theQueryBuilderto a new value. -
setParameter
Changes the parameter of the query condition with the matchingaliasto a newvalue.- Parameters:
alias- as defined usingPropertyQueryCondition.alias(String).value- The new value to use for the query condition.
-
setParameter
Sets a parameter previously given to theQueryBuilderto a new value. -
setParameter
Changes the parameter of the query condition with the matchingaliasto a newvalue.- Parameters:
alias- as defined usingPropertyQueryCondition.alias(String).value- The new value to use for the query condition.
-
setParameter
Sets a parameter previously given to theQueryBuilderto a new value. -
setParameter
Changes the parameter of the query condition with the matchingaliasto a newvalue.- Parameters:
alias- as defined usingPropertyQueryCondition.alias(String).value- The new value to use for the query condition.
-
setParameter
Sets a parameter previously given to theQueryBuilderto a new value.- Throws:
NullPointerException- if given date is null
-
setParameter
Changes the parameter of the query condition with the matchingaliasto a newvalue.- Parameters:
alias- as defined usingPropertyQueryCondition.alias(String).value- The new value to use for the query condition.- Throws:
NullPointerException- if given date is null
-
setParameter
Sets a parameter previously given to theQueryBuilderto a new value. -
setParameter
Changes the parameter of the query condition with the matchingaliasto a newvalue.- Parameters:
alias- as defined usingPropertyQueryCondition.alias(String).value- The new value to use for the query condition.
-
setParameter
Changes the parameter of the query condition forpropertyto a newvalue.- Parameters:
property- Property reference from generated entity underscore class, likeExample_.example.value- The newint[]value to use for the query condition.
-
setParameter
Changes the parameter of the query condition with the matchingaliasto a newvalue.- Parameters:
alias- as defined usingPropertyQueryCondition.alias(String).value- The newint[]value to use for the query condition.
-
setParameter
Changes the parameter of the query condition forpropertyto a newvalue.- Parameters:
property- Property reference from generated entity underscore class, likeExample_.example.value- The newlong[]value to use for the query condition.
-
setParameter
Changes the parameter of the query condition with the matchingaliasto a newvalue.- Parameters:
alias- as defined usingPropertyQueryCondition.alias(String).value- The newlong[]value to use for the query condition.
-
setParameter
Changes the parameter of the query condition forpropertyto a newvalue.- Parameters:
property- Property reference from generated entity underscore class, likeExample_.example.value- The newfloat[]value to use for the query condition.
-
setParameter
Changes the parameter of the query condition with the matchingaliasto a newvalue.- Parameters:
alias- as defined usingPropertyQueryCondition.alias(String).value- The newfloat[]value to use for the query condition.
-
setParameter
Changes the parameter of the query condition forpropertyto a newvalue.- Parameters:
property- Property reference from generated entity underscore class, likeExample_.example.value- The newString[]value to use for the query condition.
-
setParameter
Changes the parameter of the query condition with the matchingaliasto a newvalue.- Parameters:
alias- as defined usingPropertyQueryCondition.alias(String).value- The newString[]value to use for the query condition.
-
setParameters
Sets a parameter previously given to theQueryBuilderto new values. -
setParameters
Changes the parameters of the query condition with the matchingaliasto the new values.- Parameters:
alias- as defined usingPropertyQueryCondition.alias(String).value1- The first value to use for the query condition.value2- The second value to use for the query condition.
-
setParameters
Deprecated.UsesetParameter(Property, int[])instead.Sets a parameter previously given to theQueryBuilderto new values. -
setParameters
Deprecated.UsesetParameter(String, int[])instead.Sets a parameter previously given to theQueryBuilderto new values.- Parameters:
alias- as defined usingQueryBuilder.parameterAlias(String).
-
setParameters
Deprecated.UsesetParameter(Property, long[])instead.Sets a parameter previously given to theQueryBuilderto new values. -
setParameters
Deprecated.UsesetParameter(String, long[])instead.Sets a parameter previously given to theQueryBuilderto new values.- Parameters:
alias- as defined usingQueryBuilder.parameterAlias(String).
-
setParameters
Sets a parameter previously given to theQueryBuilderto new values. -
setParameters
Changes the parameters of the query condition with the matchingaliasto the new values.- Parameters:
alias- as defined usingPropertyQueryCondition.alias(String).value1- The first value to use for the query condition.value2- The second value to use for the query condition.
-
setParameters
Deprecated.UsesetParameter(Property, String[])instead.Sets a parameter previously given to theQueryBuilderto new values. -
setParameters
Deprecated.UsesetParameter(String, String[])instead.Sets a parameter previously given to theQueryBuilderto new values.- Parameters:
alias- as defined usingQueryBuilder.parameterAlias(String).
-
setParameters
Sets a parameter previously given to theQueryBuilderto new values. -
setParameters
Changes the parameters of the query condition with the matchingaliasto the new values.- Parameters:
alias- as defined usingPropertyQueryCondition.alias(String).key- The first value to use for the query condition.value- The second value to use for the query condition.
-
setParameter
Sets a parameter previously given to theQueryBuilderto new values. -
setParameter
Changes the parameter of the query condition with the matchingaliasto a newvalue.- Parameters:
alias- as defined usingPropertyQueryCondition.alias(String).value- The new value to use for the query condition.
-
remove
public long remove()Removes (deletes) all Objects matching the query- Returns:
- count of removed Objects
-
subscribe
ADataObservercan be subscribed to data changes using the returned builder. The observer is supplied viaSubscriptionBuilder.observer(DataObserver)and will be notified once the query results have (potentially) changed.With subscribing, the observer will immediately get current query results. The query is run for the subscribing observer.
Threading notes: Query observers are notified from a thread pooled. Observers may be notified in parallel. The notification order is the same as the subscription order, although this may not always be guaranteed in the future.
Stale observers: you must hold on to the Query or
DataSubscriptionobjects to keep yourDataObservers active. If this Query is not referenced anymore (along with itsDataSubscriptions, which hold a reference to the Query internally), it may be GCed and observers may become stale (won't receive anymore data). -
subscribe
Convenience forsubscribe()with a subsequent call toSubscriptionBuilder.dataSubscriptionList(DataSubscriptionList).- Parameters:
dataSubscriptionList- the resultingDataSubscriptionwill be added to it
-
publish
public void publish()Publishes the current data to all subscribed @DataObservers. This is useful triggering observers when new parameters have been set. Note, that setParameter methods will NOT be propagated to observers. -
describe
For logging and testing, returns a string describing this query like "Query for entity Example with 4 conditions with properties prop1, prop2".Note: the format of the returned string may change without notice.
-
describeParameters
For logging and testing, returns a string describing the conditions of this query like "(prop1 == A AND prop2 is null)".Note: the format of the returned string may change without notice.
-
setParameter(Property, int[])instead.