• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

CascadeType 자식 엔티티 삭제

24.03.13 17:07 작성 조회수 70

0

public class JpaMain {

    public static void main(String[] args) {

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tr = em.getTransaction();
        tr.begin();
        try{

            Parent parent = new Parent();
            Child child = new Child();
            Child child1 = new Child();
            parent.addChild(child);
            parent.addChild(child1);

            em.persist(parent);

            em.flush();
            em.clear();

            Parent newParent = em.find(Parent.class, parent.getId());
            Child child2 = newParent.getChildList().get(0);
            em.remove(child2);
            tr.commit();
        }
        catch (Exception e){
            tr.rollback();
            e.printStackTrace();
        }
        finally {
            em.close();
        }
        emf.close();
    }
}


안녕하세요 CascadeType 관련해서 질문을 남깁니다. Parent에 CascadeType을 ALL 또는 PERSIST로 했을 때 위에서 child 삭제 쿼리가 나가지 않습니다. 다른 속성들은 모두 잘 동작을 하는데 왜 저 두 속성만 안되는건지 혹시 알 수 있을까요?

답변 1

답변을 작성해보세요.

0

안녕하세요. ..님

다음을 참고해주세요.

https://www.inflearn.com/questions/56718

감사합니다.