Class Box<T>
- java.lang.Object
-
- io.objectbox.Box<T>
-
@ThreadSafe public class Box<T> extends java.lang.Object
A Box to put and get Objects of a specific Entity class.Thread-safe.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
attach(T entity)
void
closeThreadResources()
LikeBoxStore.closeThreadResources()
, but limited to only this Box.boolean
contains(long id)
Check if an object with the given ID exists in the database.long
count()
Returns the count of all stored objects in this box.long
count(long maxCount)
Returns the count of all stored objects in this box or the given maxCount, whichever is lower.T
get(long id)
Get the stored object for the given ID.java.util.List<T>
get(long[] ids)
Get the stored objects for the given IDs.java.util.List<T>
get(java.lang.Iterable<java.lang.Long> ids)
Get the stored objects for the given IDs.java.util.List<T>
getAll()
Returns all stored Objects in this Box.java.lang.Class<T>
getEntityClass()
EntityInfo<T>
getEntityInfo()
long
getId(T entity)
java.util.Map<java.lang.Long,T>
getMap(java.lang.Iterable<java.lang.Long> ids)
Get the stored objects for the given IDs as a Map with IDs as keys, and entities as values.java.lang.String
getReaderDebugInfo()
java.util.List<T>
getRelationBacklinkEntities(RelationInfo<T,?> relationInfo, long id)
Given a ToMany relation and the ID of a target entity gets all source entities pointing to this target entity, for examplecustomerBox.getRelationEntities(Customer_.orders, order.getId())
.long[]
getRelationBacklinkIds(RelationInfo<T,?> relationInfo, long id)
LikegetRelationBacklinkEntities(RelationInfo, long)
, but only returns the IDs of the source entities.java.util.List<T>
getRelationEntities(RelationInfo<?,T> relationInfo, long id)
Given a ToMany relation and the ID of a source entity gets the target entities of the relation from their box, for exampleorderBox.getRelationEntities(Customer_.orders, customer.getId())
.long[]
getRelationIds(RelationInfo<?,T> relationInfo, long id)
LikegetRelationEntities(RelationInfo, long)
, but only returns the IDs of the target entities.BoxStore
getStore()
<RESULT> RESULT
internalCallWithReaderHandle(io.objectbox.internal.CallWithHandle<RESULT> task)
<RESULT> RESULT
internalCallWithWriterHandle(io.objectbox.internal.CallWithHandle<RESULT> task)
java.util.List<T>
internalGetBacklinkEntities(int entityId, Property<?> relationIdProperty, long key)
java.util.List<T>
internalGetRelationEntities(int sourceEntityId, int relationId, long key, boolean backlink)
long[]
internalGetRelationIds(int sourceEntityId, int relationId, long key, boolean backlink)
boolean
isEmpty()
Returns true if no objects are in this box.long
panicModeRemoveAll()
WARNING: this method should generally be avoided as it is not transactional and thus may leave the DB in an inconsistent state.void
put(java.util.Collection<T> entities)
Puts the given entities in a box using a single transaction.long
put(T entity)
Puts the given object and returns its (new) ID.void
put(T... entities)
Puts the given entities in a box using a single transaction.void
putBatched(java.util.Collection<T> entities, int batchSize)
Puts the given entities in a box in batches using a separate transaction for each batch.QueryBuilder<T>
query()
Create a query with no conditions.QueryBuilder<T>
query(QueryCondition<T> queryCondition)
Applies the given query conditions and returns the builder for further customization, such as result order.boolean
remove(long id)
Removes (deletes) the object with the given ID.void
remove(long... ids)
Likeremove(long)
, but removes multiple objects in a single transaction.void
remove(java.util.Collection<T> objects)
Likeremove(Object)
, but removes multiple objects in a single transaction.boolean
remove(T object)
Likeremove(long)
, but obtains the ID from the@Id
property of the given object instead.void
remove(T... objects)
Likeremove(Object)
, but removes multiple objects in a single transaction.void
removeAll()
Likeremove(long)
, but removes all objects in a single transaction.void
removeByIds(java.util.Collection<java.lang.Long> ids)
Likeremove(long)
, but removes multiple objects in a single transaction.void
removeByKeys(java.util.Collection<java.lang.Long> ids)
Deprecated.useremoveByIds(Collection)
instead.
-
-
-
Method Detail
-
closeThreadResources
public void closeThreadResources()
LikeBoxStore.closeThreadResources()
, but limited to only this Box.Rule of thumb: prefer
BoxStore.closeThreadResources()
unless you know that your thread only interacted with this Box.
-
get
public T get(long id)
Get the stored object for the given ID.- Returns:
- null if not found
-
get
public java.util.List<T> get(java.lang.Iterable<java.lang.Long> ids)
Get the stored objects for the given IDs.- Returns:
- null if not found
-
get
public java.util.List<T> get(long[] ids)
Get the stored objects for the given IDs.- Returns:
- null if not found
-
getMap
public java.util.Map<java.lang.Long,T> getMap(java.lang.Iterable<java.lang.Long> ids)
Get the stored objects for the given IDs as a Map with IDs as keys, and entities as values. IDs for which no entity is found will be put in the map with null values.- Returns:
- null if not found
-
count
public long count()
Returns the count of all stored objects in this box.
-
count
public long count(long maxCount)
Returns the count of all stored objects in this box or the given maxCount, whichever is lower.- Parameters:
maxCount
- maximum value to count or 0 (zero) to have no maximum limit
-
isEmpty
public boolean isEmpty()
Returns true if no objects are in this box.
-
getAll
public java.util.List<T> getAll()
Returns all stored Objects in this Box.- Returns:
- since 2.4 the returned list is always mutable (before an empty result list was immutable)
-
contains
public boolean contains(long id)
Check if an object with the given ID exists in the database. This is more efficient than aget(long)
and comparing against null.- Returns:
- true if an object with the given ID was found, false otherwise.
- Since:
- 2.7
-
put
public long put(T entity)
Puts the given object and returns its (new) ID.This means that if its
@Id
property is 0 or null, it is inserted as a new object and assigned the next available ID. For example, if there is an object with ID 1 and another with ID 100, it will be assigned ID 101. The new ID is also set on the given object before this returns.If instead the object has an assigned ID set, if an object with the same ID exists it is updated. Otherwise, it is inserted with that ID.
If the ID was not assigned before an
IllegalArgumentException
is thrown.When the object contains
ToOne
orToMany
relations, they are created (or updated) to point to the (new) target objects. The target objects themselves are typically not updated or removed. To do so, put or remove them using theirBox
. However, for convenience, if a target object is new, it will be inserted and assigned an ID in its Box before creating or updating the relation. Also, for ToMany relations based on aBacklink
the target objects are updated (to store changes in the linked ToOne or ToMany relation).Performance note: if you want to put several objects, consider
put(Collection)
,put(Object[])
,BoxStore.runInTx(Runnable)
, etc. instead.
-
put
@SafeVarargs public final void put(@Nullable T... entities)
Puts the given entities in a box using a single transaction.See
put(Object)
for more details.
-
put
public void put(@Nullable java.util.Collection<T> entities)
Puts the given entities in a box using a single transaction.See
put(Object)
for more details.- Parameters:
entities
- It is fine to pass null or an empty collection: this case is handled efficiently without overhead.
-
putBatched
public void putBatched(@Nullable java.util.Collection<T> entities, int batchSize)
Puts the given entities in a box in batches using a separate transaction for each batch.See
put(Object)
for more details.- Parameters:
entities
- It is fine to pass null or an empty collection: this case is handled efficiently without overhead.batchSize
- Number of entities that will be put in one transaction. Must be 1 or greater.
-
remove
public boolean remove(long id)
Removes (deletes) the object with the given ID.If the object is part of a relation, it will be removed from that relation as well.
- Returns:
- true if the object did exist and was removed, otherwise false.
-
remove
public void remove(@Nullable long... ids)
Likeremove(long)
, but removes multiple objects in a single transaction.
-
removeByKeys
@Deprecated public void removeByKeys(@Nullable java.util.Collection<java.lang.Long> ids)
Deprecated.useremoveByIds(Collection)
instead.
-
removeByIds
public void removeByIds(@Nullable java.util.Collection<java.lang.Long> ids)
Likeremove(long)
, but removes multiple objects in a single transaction.
-
remove
public boolean remove(T object)
Likeremove(long)
, but obtains the ID from the@Id
property of the given object instead.
-
remove
@SafeVarargs public final void remove(@Nullable T... objects)
Likeremove(Object)
, but removes multiple objects in a single transaction.
-
remove
public void remove(@Nullable java.util.Collection<T> objects)
Likeremove(Object)
, but removes multiple objects in a single transaction.
-
removeAll
public void removeAll()
Likeremove(long)
, but removes all objects in a single transaction.
-
panicModeRemoveAll
@Experimental public long panicModeRemoveAll()
WARNING: this method should generally be avoided as it is not transactional and thus may leave the DB in an inconsistent state. It may be the a last resort option to recover from a full DB. Like removeAll(), it removes all objects, returns the count of objects removed. Logs progress using warning log level.
-
query
public QueryBuilder<T> query()
Create a query with no conditions.- See Also:
query(QueryCondition)
-
query
public QueryBuilder<T> query(QueryCondition<T> queryCondition)
Applies the given query conditions and returns the builder for further customization, such as result order. Build the condition using the properties from your entity underscore classes.An example with a nested OR condition:
# Java box.query(User_.name.equal("Jane") .and(User_.age.less(12) .or(User_.status.equal("child")))); # Kotlin box.query(User_.name.equal("Jane") and (User_.age.less(12) or User_.status.equal("child")))
This method is a shortcut forquery().apply(condition)
.- See Also:
QueryBuilder.apply(QueryCondition)
-
getStore
public BoxStore getStore()
-
getEntityInfo
public EntityInfo<T> getEntityInfo()
-
getEntityClass
public java.lang.Class<T> getEntityClass()
-
internalGetBacklinkEntities
@Internal public java.util.List<T> internalGetBacklinkEntities(int entityId, Property<?> relationIdProperty, long key)
-
internalGetRelationEntities
@Internal public java.util.List<T> internalGetRelationEntities(int sourceEntityId, int relationId, long key, boolean backlink)
-
internalGetRelationIds
@Internal public long[] internalGetRelationIds(int sourceEntityId, int relationId, long key, boolean backlink)
-
getRelationEntities
public java.util.List<T> getRelationEntities(RelationInfo<?,T> relationInfo, long id)
Given a ToMany relation and the ID of a source entity gets the target entities of the relation from their box, for exampleorderBox.getRelationEntities(Customer_.orders, customer.getId())
.
-
getRelationBacklinkEntities
public java.util.List<T> getRelationBacklinkEntities(RelationInfo<T,?> relationInfo, long id)
Given a ToMany relation and the ID of a target entity gets all source entities pointing to this target entity, for examplecustomerBox.getRelationEntities(Customer_.orders, order.getId())
.
-
getRelationIds
public long[] getRelationIds(RelationInfo<?,T> relationInfo, long id)
LikegetRelationEntities(RelationInfo, long)
, but only returns the IDs of the target entities.
-
getRelationBacklinkIds
public long[] getRelationBacklinkIds(RelationInfo<T,?> relationInfo, long id)
LikegetRelationBacklinkEntities(RelationInfo, long)
, but only returns the IDs of the source entities.
-
internalCallWithReaderHandle
@Internal public <RESULT> RESULT internalCallWithReaderHandle(io.objectbox.internal.CallWithHandle<RESULT> task)
-
internalCallWithWriterHandle
@Internal public <RESULT> RESULT internalCallWithWriterHandle(io.objectbox.internal.CallWithHandle<RESULT> task)
-
getReaderDebugInfo
public java.lang.String getReaderDebugInfo()
-
-