ToOneclass, a smart proxy to the target object. It gets and caches the target object transparently. For example, an order is typically made by one customer. Thus, we could model the
Orderclass to have a to-one relation to the
targetin Kotlin) on the ToOne instance and put the order object:
ToOnewill put it (so there will be two puts, one for
Order, one for
Customer). If it already exists,
ToOnewill only create the relation (so there's only one put for
Order, as explicitly written in the code). See further below for details about updating relations.
lib/bjectbox-model.jsonin Dart) you can see, a ToOne property is not actually stored. Instead, the ID of the target object is saved in a virtual property named like the ToOne property appended with Id.
Longin Kotlin) property in your entity class with the expected name:
customerwas never initialized in the code example above? Why can the code still use
customerwithout any NullPointerException? Because the field actually is initialized – the initialization code just is not visible in your sources.
Listproperties have been initialized and are ready for you to use.
Id. Check your
objectbox-models/default.jsonfile to find the correct name.
ToMany. As the ToOne class, the
ToManyclass helps you to keep track of changes and to apply them to the database.
ToManyobject. So subsequent calls to any method, like
ToMany, do not query the database, even if the relation was changed elsewhere. To get the latest data fetch the source entity again or call
ToManyimplements the Java List interface, so we can simply add orders to it:
ToManywill put them. If they already exist, it will only create the relation (but not put them). See further below for details about updating relations.
ToManyclass. Assuming a students and teachers example, this is how a simple student class that has a to-many relation to teachers can look like:
ToManywill also put them. If they already exist,
ToManywill only create the relation (but not put them). See further below for details about updating relations.
ToManytype it is also possible to use
MutableListin Kotlin) for a to-many property. At runtime the property will still be a
ToManydoes implement the
Listinterface) due to the initialization magic described above, or if manually initialized as seen in the example below.
@Id(assignable = true)), only the relation will be created or destroyed (internally the Object ID is added to or removed from the relation). In that case, to put changes to the properties of related Objects use their specific Box instead:
setTarget(target)makes the given (new or existing) Object the new relation target; pass
nullto clear the relation.
setTargetId(targetId)sets the relation target based on the given ID of an existing Object; pass
0(zero) to clear the relation.
setAndPutTarget(target)makes the given (new or existing) Object the new relation target and puts the owning, source Object and if needed the target Object.
ToManyrelation class is based on a standard
Listwith added change tracking for Objects. As mentioned above, it will put new Objects (ID == 0) that are added to it once the Object owning it is put. And when removing Objects from it, just the relation is cleared, the Objects are not removed from their Box.
@Id(assignable = true)attach its Box before modifying its ToMany:
Orderabove, is using self-assigned IDs put the target Objects before adding them to the ToMany relation:
Addresstype. While you could model street, ZIP and so on directly into
Customer, it usually makes sense to normalize that out into a separate entity. And when you think about the usual shopping sites, they all let you have multiple addresses...
ToOneyou could have a matching
ToManyon the other side of the relation (backlink)