From 698623f2a4520504be78b917cd7f2d0ff6dbf541 Mon Sep 17 00:00:00 2001 From: haerong22 Date: Mon, 30 Jan 2023 01:47:25 +0900 Subject: [PATCH] #30 jpa basic: MappedSuperClass --- .../jpa/inheritance/ex01/BaseEntity.java | 45 ++++++++++++++++++ .../hello/jpa/inheritance/ex01/JpaMain.java | 28 +++++++++++ .../hello/jpa/inheritance/ex01/Member.java | 42 +++++++++++++++++ .../hello/jpa/inheritance/ex01/RoleType.java | 6 +++ .../com/hello/jpa/inheritance/ex01/Team.java | 47 +++++++++++++++++++ .../main/resources/META-INF/persistence.xml | 10 ++-- 6 files changed, 174 insertions(+), 4 deletions(-) create mode 100644 hello-jpa/src/main/java/com/hello/jpa/inheritance/ex01/BaseEntity.java create mode 100644 hello-jpa/src/main/java/com/hello/jpa/inheritance/ex01/JpaMain.java create mode 100644 hello-jpa/src/main/java/com/hello/jpa/inheritance/ex01/Member.java create mode 100644 hello-jpa/src/main/java/com/hello/jpa/inheritance/ex01/RoleType.java create mode 100644 hello-jpa/src/main/java/com/hello/jpa/inheritance/ex01/Team.java diff --git a/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex01/BaseEntity.java b/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex01/BaseEntity.java new file mode 100644 index 00000000..34544914 --- /dev/null +++ b/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex01/BaseEntity.java @@ -0,0 +1,45 @@ +package com.hello.jpa.inheritance.ex01; + +import javax.persistence.MappedSuperclass; +import java.time.LocalDateTime; + +@MappedSuperclass +public abstract class BaseEntity { + + private String createdBy; + private LocalDateTime createdAt; + private String updatedBy; + private LocalDateTime updatedAt; + + public String getCreatedBy() { + return createdBy; + } + + public void setCreatedBy(String createdBy) { + this.createdBy = createdBy; + } + + public LocalDateTime getCreatedAt() { + return createdAt; + } + + public void setCreatedAt(LocalDateTime createdAt) { + this.createdAt = createdAt; + } + + public String getUpdatedBy() { + return updatedBy; + } + + public void setUpdatedBy(String updatedBy) { + this.updatedBy = updatedBy; + } + + public LocalDateTime getUpdatedAt() { + return updatedAt; + } + + public void setUpdatedAt(LocalDateTime updatedAt) { + this.updatedAt = updatedAt; + } +} diff --git a/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex01/JpaMain.java b/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex01/JpaMain.java new file mode 100644 index 00000000..95c3c87f --- /dev/null +++ b/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex01/JpaMain.java @@ -0,0 +1,28 @@ +package com.hello.jpa.inheritance.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 { + tx.commit(); + } catch (Exception e) { + tx.rollback(); + } finally { + em.close(); + } + + emf.close(); + } +} diff --git a/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex01/Member.java b/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex01/Member.java new file mode 100644 index 00000000..cd9a761f --- /dev/null +++ b/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex01/Member.java @@ -0,0 +1,42 @@ +package com.hello.jpa.inheritance.ex01; + +import javax.persistence.*; + +@Entity +public class Member extends BaseEntity { + + @Id @GeneratedValue + @Column(name = "MEMBER_ID") + private Long id; + + @Column(name = "USERNAME") + private String name; + + @ManyToOne + @JoinColumn(name = "TEAM_ID") + private Team team; + + 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 Team getTeam() { + return team; + } + + public void setTeam(Team team) { + this.team = team; + } +} diff --git a/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex01/RoleType.java b/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex01/RoleType.java new file mode 100644 index 00000000..fcc953fc --- /dev/null +++ b/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex01/RoleType.java @@ -0,0 +1,6 @@ +package com.hello.jpa.inheritance.ex01; + +public enum RoleType { + + USER, ADMIN +} diff --git a/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex01/Team.java b/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex01/Team.java new file mode 100644 index 00000000..40e6d2b2 --- /dev/null +++ b/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex01/Team.java @@ -0,0 +1,47 @@ +package com.hello.jpa.inheritance.ex01; + +import javax.persistence.*; +import java.util.ArrayList; +import java.util.List; + +@Entity +public class Team extends BaseEntity { + + @Id @GeneratedValue + @Column(name = "TEAM_ID") + private Long id; + + private String name; + + @OneToMany(mappedBy = "team") + private List members = new ArrayList<>(); + + public void addMember(Member member) { + member.setTeam(this); + members.add(member); + } + + 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 getMembers() { + return members; + } + + public void setMembers(List members) { + this.members = members; + } +} diff --git a/hello-jpa/src/main/resources/META-INF/persistence.xml b/hello-jpa/src/main/resources/META-INF/persistence.xml index 767bc14b..2cee8efa 100644 --- a/hello-jpa/src/main/resources/META-INF/persistence.xml +++ b/hello-jpa/src/main/resources/META-INF/persistence.xml @@ -24,10 +24,12 @@ - com.hello.jpa.inheritance.ex00.Item - com.hello.jpa.inheritance.ex00.Album - com.hello.jpa.inheritance.ex00.Book - com.hello.jpa.inheritance.ex00.Movie + + + + + com.hello.jpa.inheritance.ex01.Member + com.hello.jpa.inheritance.ex01.Team