jeudi 3 septembre 2020

Composite Relationship assign parent reference to child

I have a class like:

@Setter
@Getter
public class Parent {
    private String Name;
    private String lastName;
    private String address;
    private Child child;
    
    public Parent() {
    }
}

And a Child in a composite relation like:

@Setter
@Getter
public class Child {
    private String nameChild;
    private String lastNameChild;
    private Parent parent;
}

What I want:

@Test
    public void Test() {
        Parent parent = new Parent();
        parent.setAddress("Address");
        parent.setLastName("LastName");
        parent.setName("Name");

        parent.getChild().setNameChild("ChildName");
        parent.getChild().setLastNameChild("ChildLastName");

        Parent referenceToParent = parent.getChild().getParent();
    }

I want to get in referenceToParent the reference to the parent from the child in a composite relationship.

How can I structure my object?

Aucun commentaire:

Enregistrer un commentaire