스프링 부트/스프링 데이터에 문제가있어 중첩 엔티티를 한 방향으로 만 만들 수 있습니다.스프링 데이터 나머지 - 한 방향으로 만 중첩 된 엔티티 만 생성 할 수 있습니다.
두 개의 엔터티 인 Parent and Child가 있고 Parent에는 Child 엔터티와 OnetoMany 관계가 있습니다.
나는이 일을하여 아이를 만드는 경우 :
POST http://localhost:8080/children
{
"name": "Tenzin"
}
하고이 일을하여 부모를 만듭니다
POST http://localhost:8080/parents
{
"name": "Fred and Alma",
"children": [ "http://localhost:8080/children/1" ]
}
작동하지 않습니다. 그러나
, 내가 먼저 부모를 만든 다음이 작업을 수행하여 새로운 아이를 작성하는 경우, 그것은하지 작업 :
POST http://localhost:8080/children
{
"name": "Jacob",
"parent": [ "http://localhost:8080/parents/1" ]
}
,이 경우는 왜 이 예상되는 동작입니다 또는 I 오전 뭔가 잘못하고 있는거야?
하위 엔티티 (아래 참조)가 하위 속성에서 모두 캐스케이드 = ALL을 갖고 있기 때문에 그렇습니까?
부모 엔티티 :
@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;
}
}
아이 엔티티 : 당신이 예에서
@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;
}
}
repos를 표시 할 수 있습니까? – Cepr0
안녕 Cepr0, 그것 : https://github.com/simbro/spring-boot-practice-application - 비록 내가 알아 낸 것 같아요 - 아래 참조하십시오. – simbro