David Novák
David Novák

Reputation: 1495

Realm android one to many - cannont fetch children by parent

I have Parent and Child class:

public class Parent extends RealmObject {
    private String name;
    private RealmList<Child> children;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public RealmList<Child> getChildren() {
        return children;
    }

    @Override
    public String toString() {
        return this.name;
    }
}


public class Child extends RealmObject {
    private String name;
    private Parent parent;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Parent getParent() {
        return parent;
    }

    public void setParent(Parent parent) {
        this.parent = parent;
    }

    @Override
    public String toString() {
        return this.name;
    }
}

Now, I create an example data:

    realm.beginTransaction();
    realm.deleteAll();

    Child child1 = realm.createObject(Child.class);
    child1.setName("Bart");

    Child child2 = realm.createObject(Child.class);
    child2.setName("Lisa");

    Child child3 = realm.createObject(Child.class);
    child3.setName("Maggie");

    Parent parent = realm.createObject(Parent.class);
    parent.setName("Homer");

    child1.setParent(parent);
    child2.setParent(parent);
    child3.setParent(parent);

    realm.commitTransaction();

Why am I not getting children when querying like this?

    Parent homer = realm.where(Parent.class).findFirst();
    for(Child child : homer.getChildren()) {
        System.out.println(child.toString());
    }

It works when I'm assigning children to parent via parent.getChildren().add(child). But it does not work when I'm assigning parent to children (like child.setParent(parent)). What am I doing wrong here?

Upvotes: 0

Views: 580

Answers (2)

EpicPandaForce
EpicPandaForce

Reputation: 81539

Because you need to add the children to the parent links.

Setting the children's link to the parent is a completely independent thing from the link in the parent to the children.

realm.beginTransaction();
realm.deleteAll();

Child child1 = realm.createObject(Child.class);
child1.setName("Bart");

Child child2 = realm.createObject(Child.class);
child2.setName("Lisa");

Child child3 = realm.createObject(Child.class);
child3.setName("Maggie");

Parent parent = realm.createObject(Parent.class);
parent.setName("Homer");

child1.setParent(parent);
child2.setParent(parent);
child3.setParent(parent);

parent.getChildren().add(child1);
parent.getChildren().add(child2);
parent.getChildren().add(child3);

realm.commitTransaction();

Upvotes: 0

Juuso
Juuso

Reputation: 485

This does not work because even though you are setting a parent for the child, there exists no link to the corresponding list of children in the parent object ("the parent does not know that there is now a child somewhere calling him their parent"). I am not sure it is even possible to have a bi-directional relationship between objects like this in Realm.

Depending on what you want to achieve, you could try querying for the children:

Child child = realm.where(Child.class).equalTo("parent.name","homer").findAll();

Upvotes: 2

Related Questions