강의

멘토링

로드맵

Inflearn brand logo image

인프런 커뮤니티 질문&답변

작성자 없음

작성자 정보가 삭제된 글입니다.

자바 ORM 표준 JPA 프로그래밍 - 기본편

영속성 전이(CASCADE)와 고아 객체

왜 delete 쿼리가 안나가는 걸까요?

작성

·

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이 남아있는데 혹시 왜 그런지 알 수 있을까요?

 

 

답변 1

0

안녕하세요. 내가바로김민규님, 공식 서포터즈 David입니다.

orphanRemoval 옵션이 누락된 것으로 보입니다.

12분 37초부터 설명하고 있으니 참고해 주세요:)

감사합니다.

작성자 없음

작성자 정보가 삭제된 글입니다.

질문하기