Skip to content

Instantly share code, notes, and snippets.

@PierceZ
Last active September 5, 2017 06:07
Show Gist options
  • Save PierceZ/248d3e8d99c9bf3a3c6fa1ec10621f4d to your computer and use it in GitHub Desktop.
Save PierceZ/248d3e8d99c9bf3a3c6fa1ec10621f4d to your computer and use it in GitHub Desktop.
An example of how to use relations in ObjectBox.
Zoo myZoo = new Zoo();
Animal elephant = new Animal();
Animal giraffe = new Animal();
// To-one relation: Set the Zoo that an animal belongs to and save it to the database
elephant.zoo.setTarget(myZoo);
animalBox.put(elephant);
// To-one relation: Get the Zoo that an animal belongs to
Zoo elephantZoo = elephant.zoo.getTarget();
// To-many relation: Add Animals to the Zoo and save it to the database
myZoo.animals.add(elephant);
myZoo.animals.add(giraffe);
zooBox.put(myZoo);
// To-many relation: Get Animals that belongs to a Zoo
List<Animal> zooAnimals = myZoo.animals;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment