#30 jpa basic: MappedSuperClass

This commit is contained in:
haerong22
2023-01-30 01:47:25 +09:00
parent 499ad11274
commit 698623f2a4
6 changed files with 174 additions and 4 deletions

View File

@@ -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;
}
}

View File

@@ -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();
}
}

View File

@@ -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;
}
}

View File

@@ -0,0 +1,6 @@
package com.hello.jpa.inheritance.ex01;
public enum RoleType {
USER, ADMIN
}

View File

@@ -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<Member> 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<Member> getMembers() {
return members;
}
public void setMembers(List<Member> members) {
this.members = members;
}
}

View File

@@ -24,10 +24,12 @@
<!-- <class>com.hello.jpa.relationmapping.ex03.domain.Member</class>-->
<!-- <class>com.hello.jpa.relationmapping.ex03.domain.Order</class>-->
<!-- <class>com.hello.jpa.relationmapping.ex03.domain.OrderItem</class>-->
<class>com.hello.jpa.inheritance.ex00.Item</class>
<class>com.hello.jpa.inheritance.ex00.Album</class>
<class>com.hello.jpa.inheritance.ex00.Book</class>
<class>com.hello.jpa.inheritance.ex00.Movie</class>
<!-- <class>com.hello.jpa.inheritance.ex00.Item</class>-->
<!-- <class>com.hello.jpa.inheritance.ex00.Album</class>-->
<!-- <class>com.hello.jpa.inheritance.ex00.Book</class>-->
<!-- <class>com.hello.jpa.inheritance.ex00.Movie</class>-->
<class>com.hello.jpa.inheritance.ex01.Member</class>
<class>com.hello.jpa.inheritance.ex01.Team</class>
<properties>
<!-- 필수 속성 -->