작성자 없음
작성자 정보가 삭제된 글입니다.
작성
·
318
0
Parent class
@Id
@GeneratedValue
private Long id;
private String name;
@OneToMany(mappedBy = "parent",cascade = CascadeType.ALL)
private List<Child> childList = new ArrayList<>();
public void addChild(Child... childArray) {
for (Child child : childArray) {
child.setParent(this);
childList.add(child);
}
}
Child class
@Setter
@Id
@GeneratedValue
private Long id;
private String name;
@ManyToOne
@JoinColumn(name = "PARENT_ID")
private Parent parent;
Main 메서드
Parent parent = new Parent();
Child child = new Child();
Child child1 = new Child();
parent.addChild(child, child1);
em.persist(parent);
em.flush();
em.clear();
Parent findParent = em.find(Parent.class, parent.getId());
Child findChild = findParent.getChildList().get(0);
em.remove(findChild);
tx.commit();
Main메서드에서 findParent에서 자식 리스트를 가져와 첫번째 자식을 삭제해도 db에는 여전히 child, child1이 남아있는데 혹시 왜 그런지 알 수 있을까요?