Java – How to get the parent object in Realm

How to get the parent object in Realm… here is a solution to the problem.

How to get the parent object in Realm

Reference Realm document

I have some entities such as: Category and Item.
The category contains RealmList’s Items and I can access all items in that category by calling the list’s getter.
But how do I query all items
by the id of the category (which is annotated as the primary key).
I’m parsing json via Realm.createObjectFromJson() and can’t set the Category field for each Item
Thanks in advance


Without any link from your Item to your Category, you can’t currently query items by category. The concept you are looking for on our TODO is called backlinks. You can follow the progress here:

The current workaround is to manually create the link after copying them to Realm:

Category category = realm.createObjectFromJson(categoryJson);
for (Item item : category.getItems()) {

 Then you can do
realm.where(Item.class).equalTo("", category.getId()).findAll();

Related Problems and Solutions