Spring data jpa insert child into parent using rest call

Viewed 167

Let's assume we have two entities A and B which have such a relation:

public class A {

    @Id
    private Integer id;

    @OneToMany(mappedBy = "parent")
    private Set<B> childs = new HashSet<>();

    // getters and setters
}

public class B {

    @Id
    private Integer id;

    @ManyToOne
    private A parent;

    // getters and setters
}

I'm having 2 repositories, one for each entity (ARepository and BRepository) and i'm using spring data rest starter so i've got generated rest api.

Let assume we have an instance of A, which has as id=12

In order to insert a B entity and link it to A i have to: POST the url http://localhost:8080/appContext/b/ and giving it a request body as follow: {"parent": "a/12"}

It works fine, but it is possible to have the same thing by posting http://localhost:8080/appContext/a/12/childs/ with the same request param except the "parent" JSON attribute ? as it's actually possible to fetch the childs of a parent using this last url.

0 Answers
Related