Class ToMany<TARGET>
- Type Parameters:
TARGET
- target object type (@Entity
class).
- All Implemented Interfaces:
Serializable
,Iterable<TARGET>
,Collection<TARGET>
,List<TARGET>
ToMany
entity.
Example:
// Java
@Entity
public class Student {
private ToMany<Teacher> teachers;
}
// Kotlin
@Entity
data class Student() {
lateinit var teachers: ToMany<Teacher>
}
Implements the List
interface and uses lazy initialization. The target objects are only read from the
database when the list is first accessed.
The required database query runs on the calling thread, so avoid accessing ToMany from a UI or main thread. To get the
latest data Box.get(long)
the object with the ToMany again or use reset()
before accessing the list again.
It is possible to preload the list when running a query using QueryBuilder.eager(io.objectbox.relation.RelationInfo, io.objectbox.relation.RelationInfo...)
.
Tracks when target objects are added and removed. Common usage:
add(Object)
to add target objects to the relation.remove(Object)
to remove target objects from the relation.remove(int)
to remove target objects at a specific index.
To apply (persist) the changes to the database, call applyChangesToDb()
or put the object with the ToMany.
For important details, see the notes about relations of Box.put(Object)
.
// Example 1: add target objects to a relation
student.getTeachers().add(teacher1);
student.getTeachers().add(teacher2);
store.boxFor(Student.class).put(student);
// Example 2: remove a target object from the relation
student.getTeachers().remove(index);
student.getTeachers().applyChangesToDb();
// or store.boxFor(Student.class).put(student);
In the database, the target objects are referenced by their IDs, which are persisted as part of the relation of the object with the ToMany.
ToMany is thread-safe by default (may not be the case if setListFactory(ListFactory)
is used).
To get all objects with a ToMany that reference a target object, see Backlink
.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Seeadd(Object)
for general comments.boolean
Prepares to add the given target object to this relation.boolean
addAll
(int index, Collection<? extends TARGET> objects) Seeadd(Object)
for general comments.boolean
addAll
(Collection<? extends TARGET> objects) Seeadd(Object)
for general comments.void
Saves changes (added and removed objects) made to this relation to the database.void
clear()
boolean
boolean
containsAll
(Collection<?> collection) get
(int location) Gets the target object at the given index.int
getById
(long id) Gets an object by its ID.int
boolean
hasA
(QueryFilter<TARGET> filter) Returns true if at least one of the target objects matches the given filter.boolean
hasAll
(QueryFilter<TARGET> filter) Returns true if all of the target objects match the given filter.boolean
Returns true if there are pending changes for the DB.int
int
indexOfId
(long id) Gets the index of the object with the given ID.void
internalApplyToDb
(io.objectbox.Cursor<?> sourceCursor, io.objectbox.Cursor<TARGET> targetCursor) For internal use only; do not use in your app.boolean
For internal use only; do not use in your app.boolean
isEmpty()
boolean
iterator()
int
lastIndexOf
(Object object) listIterator
(int location) The returned iterator does not track any potential calls toIterator.remove()
.remove
(int location) Likeremove(Object)
, but using the location of the target object.boolean
Prepares to remove the target object from this relation.boolean
removeAll
(Collection<?> objects) removeById
(long id) Likeremove(Object)
, but using just the ID of the target object.void
reset()
Resets the already loaded (cached) objects of this list, so they will be re-loaded when accessing this list again.boolean
retainAll
(Collection<?> objects) void
setComparator
(Comparator<TARGET> comparator) Set an comparator to define the order of entities.void
setListFactory
(ListFactory listFactory) Currently only used for non-persisted entities (id == 0).void
setRemoveFromTargetBox
(boolean removeFromTargetBox) On put, this also deletes removed entities from the target Box.int
size()
void
sortById()
Sorts the list by the "natural" ObjectBox order for to-many list (by object ID).subList
(int start, int end) The returned sub list does not do any change tracking.Object[]
toArray()
<T> T[]
toArray
(T[] array) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
Methods inherited from interface java.util.List
equals, hashCode, replaceAll, sort, spliterator
-
Constructor Details
-
ToMany
-
-
Method Details
-
setListFactory
Currently only used for non-persisted entities (id == 0). -
setComparator
Set an comparator to define the order of entities. -
setRemoveFromTargetBox
On put, this also deletes removed entities from the target Box. Note: removed target entities won't cascade the delete. -
getListFactory
-
add
Prepares to add the given target object to this relation.To apply changes, call
applyChangesToDb()
or put the object with the ToMany. For important details, see the notes about relations ofBox.put(Object)
. -
add
Seeadd(Object)
for general comments. -
addAll
Seeadd(Object)
for general comments. -
addAll
Seeadd(Object)
for general comments. -
clear
public void clear() -
contains
-
containsAll
- Specified by:
containsAll
in interfaceCollection<TARGET>
- Specified by:
containsAll
in interfaceList<TARGET>
-
get
Gets the target object at the given index.ToMany
uses lazy initialization, so on first access this will read the target objects from the database. -
indexOf
-
isEmpty
public boolean isEmpty() -
iterator
-
lastIndexOf
- Specified by:
lastIndexOf
in interfaceList<TARGET>
-
listIterator
- Specified by:
listIterator
in interfaceList<TARGET>
-
listIterator
The returned iterator does not track any potential calls toIterator.remove()
. Thus these removes will NOT be synced to the target Box.- Specified by:
listIterator
in interfaceList<TARGET>
-
remove
Likeremove(Object)
, but using the location of the target object. -
remove
Prepares to remove the target object from this relation.To apply changes, call
applyChangesToDb()
or put the object with the ToMany. For important details, see the notes about relations ofBox.put(Object)
. -
removeById
Likeremove(Object)
, but using just the ID of the target object. -
removeAll
-
retainAll
-
set
-
size
public int size() -
subList
The returned sub list does not do any change tracking. Thus any modifications to the sublist won't be synced to the target Box. -
toArray
-
toArray
@Nonnull public <T> T[] toArray(T[] array) -
reset
public void reset()Resets the already loaded (cached) objects of this list, so they will be re-loaded when accessing this list again.Use this to sync with changes to this relation or target objects made outside of this ToMany.
-
isResolved
public boolean isResolved() -
getAddCount
public int getAddCount() -
getRemoveCount
public int getRemoveCount() -
sortById
public void sortById()Sorts the list by the "natural" ObjectBox order for to-many list (by object ID). This will be the order when you get the objects fresh (e.g. initially or after callingreset()
). Note that non persisted objects (ID is zero) will be put to the end as they are still to get an ID. -
applyChangesToDb
public void applyChangesToDb()Saves changes (added and removed objects) made to this relation to the database. For some important details, see the notes about relations ofBox.put(Object)
.Note that this is called already when the object that contains this ToMany is put. However, if only this ToMany has changed, it is more efficient to just use this method.
- Throws:
IllegalStateException
- If the object that contains this ToMany has no ID assigned (it must have been put before).
-
hasA
Returns true if at least one of the target objects matches the given filter.For use with
QueryBuilder.filter(QueryFilter)
inside aQueryFilter
to check to-many relation objects. -
hasAll
Returns true if all of the target objects match the given filter. Returns false if the list is empty.For use with
QueryBuilder.filter(QueryFilter)
inside aQueryFilter
to check to-many relation objects. -
getById
Gets an object by its ID. -
indexOfId
Gets the index of the object with the given ID. -
hasPendingDbChanges
public boolean hasPendingDbChanges()Returns true if there are pending changes for the DB. Changes will be automatically persisted once the object with the ToMany is put, or an explicit call toapplyChangesToDb()
is made. -
internalCheckApplyToDbRequired
For internal use only; do not use in your app. Called after relation source object is put (so we have its ID). Prepares data forinternalApplyToDb(Cursor, Cursor)
-
internalApplyToDb
@Internal public void internalApplyToDb(io.objectbox.Cursor<?> sourceCursor, io.objectbox.Cursor<TARGET> targetCursor) For internal use only; do not use in your app. Convention:internalCheckApplyToDbRequired()
must be called before this call as it prepares .
-