I have an issue with Spring Boot / Spring Data whereby I can only create nested entities in one direction.
I have two entities, Parent and Child, and a Parent has a OnetoMany relationship with the Child entity.
If I create a Child, by doing this:
POST http://localhost:8080/children
{
"name": "Tenzin"
}
and then create a Parent by doing this:
POST http://localhost:8080/parents
{
"name": "Fred and Alma",
"children": [ "http://localhost:8080/children/1" ]
}
it doesn't work.
However, if I create the parent first, and then create a new child by doing this, it does work:
POST http://localhost:8080/children
{
"name": "Jacob",
"parent": [ "http://localhost:8080/parents/1" ]
}
Why is this the case, is this expected behaviour or am I doing something wrong?
Is it because the Parent entity (see below) has cascade=ALL on the children property?
Parent entity:
@Entity
public class Parent {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@OneToMany(mappedBy="parent", cascade = CascadeType.ALL)
private List<Child> children = new ArrayList<>();
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<Child> getChildren() {
return children;
}
public void setChildren(List<Child> children) {
this.children = children;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Child entity:
@Entity
public class Child {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String name;
@ManyToOne
private Parent parent;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}