From 3c35236261abe89328329ce00636e42bea824a2a Mon Sep 17 00:00:00 2001 From: haerong22 Date: Wed, 1 Feb 2023 00:44:36 +0900 Subject: [PATCH] #30 jpa basic: proxy - cascade --- .../java/com/hello/jpa/proxy/ex01/Child.java | 41 ++++++++++++++++ .../com/hello/jpa/proxy/ex01/JpaMain.java | 44 +++++++++++++++++ .../java/com/hello/jpa/proxy/ex01/Parent.java | 47 +++++++++++++++++++ .../main/resources/META-INF/persistence.xml | 6 ++- 4 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 hello-jpa/src/main/java/com/hello/jpa/proxy/ex01/Child.java create mode 100644 hello-jpa/src/main/java/com/hello/jpa/proxy/ex01/JpaMain.java create mode 100644 hello-jpa/src/main/java/com/hello/jpa/proxy/ex01/Parent.java diff --git a/hello-jpa/src/main/java/com/hello/jpa/proxy/ex01/Child.java b/hello-jpa/src/main/java/com/hello/jpa/proxy/ex01/Child.java new file mode 100644 index 00000000..ec96a26b --- /dev/null +++ b/hello-jpa/src/main/java/com/hello/jpa/proxy/ex01/Child.java @@ -0,0 +1,41 @@ +package com.hello.jpa.proxy.ex01; + +import javax.persistence.*; + +@Entity +public class Child { + + @Id + @GeneratedValue + private Long id; + + private String name; + + @ManyToOne + @JoinColumn(name = "parent_id") + private Parent parent; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Parent getParent() { + return parent; + } + + public void setParent(Parent parent) { + this.parent = parent; + } +} diff --git a/hello-jpa/src/main/java/com/hello/jpa/proxy/ex01/JpaMain.java b/hello-jpa/src/main/java/com/hello/jpa/proxy/ex01/JpaMain.java new file mode 100644 index 00000000..8d6baf0b --- /dev/null +++ b/hello-jpa/src/main/java/com/hello/jpa/proxy/ex01/JpaMain.java @@ -0,0 +1,44 @@ +package com.hello.jpa.proxy.ex01; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.Persistence; + +public class JpaMain { + + public static void main(String[] args) { + EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello"); + + EntityManager em = emf.createEntityManager(); + + EntityTransaction tx = em.getTransaction(); + tx.begin(); + + try { + + Child child1 = new Child(); + Child child2 = new Child(); + + Parent parent = new Parent(); + parent.addChild(child1); + parent.addChild(child2); + + em.persist(parent); + + em.flush(); + em.clear(); + + Parent findParent = em.find(Parent.class, parent.getId()); + findParent.getChildren().remove(0); + + tx.commit(); + } catch (Exception e) { + tx.rollback(); + } finally { + em.close(); + } + + emf.close(); + } +} diff --git a/hello-jpa/src/main/java/com/hello/jpa/proxy/ex01/Parent.java b/hello-jpa/src/main/java/com/hello/jpa/proxy/ex01/Parent.java new file mode 100644 index 00000000..84d05e9e --- /dev/null +++ b/hello-jpa/src/main/java/com/hello/jpa/proxy/ex01/Parent.java @@ -0,0 +1,47 @@ +package com.hello.jpa.proxy.ex01; + +import javax.persistence.*; +import java.util.ArrayList; +import java.util.List; + +@Entity +public class Parent { + + @Id + @GeneratedValue + private Long id; + + private String name; + + @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true) + private List children = new ArrayList<>(); + + public void addChild(Child child) { + children.add(child); + child.setParent(this); + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getChildren() { + return children; + } + + public void setChildren(List children) { + this.children = children; + } +} diff --git a/hello-jpa/src/main/resources/META-INF/persistence.xml b/hello-jpa/src/main/resources/META-INF/persistence.xml index 169d50b1..d36a1ba3 100644 --- a/hello-jpa/src/main/resources/META-INF/persistence.xml +++ b/hello-jpa/src/main/resources/META-INF/persistence.xml @@ -30,8 +30,10 @@ - com.hello.jpa.proxy.ex00.Member - com.hello.jpa.proxy.ex00.Team + + + com.hello.jpa.proxy.ex01.Parent + com.hello.jpa.proxy.ex01.Child