Last active
September 5, 2017 06:07
-
-
Save PierceZ/248d3e8d99c9bf3a3c6fa1ec10621f4d to your computer and use it in GitHub Desktop.
An example of how to use relations in ObjectBox.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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