Relations
Learn how to create and update to-one and to-many relations between entities in ObjectBox C / C++ database.
Last updated
Learn how to create and update to-one and to-many relations between entities in ObjectBox C / C++ database.
Last updated
Objects may reference other objects, for example using a simple reference or a list of objects. In database terms, we call those references relations. The object defining a relation we call the source object, the referenced object we call the target object. So a relation has a direction. If there is one target object, we call the relation to-one. And if there can be multiple target objects, we call it to-many. Note, the direction of the relation matters, as there's also a link back in the reverse direction (the "backlink").
If you are familiar with 1:N and N:M relations, here are the corresponding object relations in ObjectBox:
1:N (one-to-many): to-one relation with its backlink N:M (many-to-many): to-many relation with its backlink
You define a to-one relation by using the property annotation objectbox:relation=<TargetEntity>
in the source entity definition.
You can use relations in queries to combine query conditions across multiple entity types. At the API level, this is done by following relations from the source to the target by calling a link(<RelationProperty>)
method on the Query Builder which returns new a Query Builder suitable to express conditions on the target entity.
Example for finding all "Potato" orders by customers who's name starts with "O":
A to-one relation implies a direction. For example, when an order is made by a customer, it's the order that points to a customer (via a customer ID). However, in ObjectBox, relations are always bidirectional. Thus, we can also use the reverse direction, which we call the backlink of a relation. In the case of the "order -> customer" to-one relation, its backlink is "customer -> order". Note that one customer can have multiple orders, and thus we can say that the backlink is actually a "one-to-many" (1:N) relation.
To create a query following the backlink (the "reverse" direction), you can call backlink(<RelationProperty>)
on the Query Builder starting from the "target entity" of the to-one relation. Analog to link
, a Query Builder is returned to extend the query expression by conditions on the source entity.
Example for finding all customers with their name starting with "O" and who ordered potatoes:
You define a many-to-many relation by using the entity annotation objectbox:relation(name=<SourceRelFieldName>,to=<TargetEntity>)
and apply it on source entity definition.
In contrast to to-one relations, many-to-many relations are stored "standalone" (not inside the objects themselves). Thus, a separate set of API calls exists for put, get and remove.
Example for putting a "teachers" relation from Student "Ferris" to Teacher "Rooney".
Similar to To-One relations, building queries across relation links is done using the same link
/ backlink
API style (see the to-section for basics and details).
Example for finding all students of Teacher "Rooney":
Example for finding all teachers of Student "Ferris":