#30 jpa basic: proxy - fetch type

This commit is contained in:
haerong22
2023-01-31 01:23:27 +09:00
parent 698623f2a4
commit ab9ee6cb4c
6 changed files with 191 additions and 2 deletions

View File

@@ -0,0 +1,45 @@
package com.hello.jpa.proxy.ex00;
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,47 @@
package com.hello.jpa.proxy.ex00;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import java.util.List;
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 {
Team team = new Team();
team.setName("team");
em.persist(team);
Member member = new Member();
member.setName("member");
member.setTeam(team);
em.persist(member);
em.flush();
em.clear();
Member m = em.find(Member.class, member.getId());
List<Member> members = em.createQuery("select m from Member m join fetch m.team", Member.class)
.getResultList();
tx.commit();
} catch (Exception e) {
tx.rollback();
} finally {
em.close();
}
emf.close();
}
}

View File

@@ -0,0 +1,42 @@
package com.hello.jpa.proxy.ex00;
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(fetch = FetchType.LAZY)
@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.proxy.ex00;
public enum RoleType {
USER, ADMIN
}

View File

@@ -0,0 +1,47 @@
package com.hello.jpa.proxy.ex00;
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

@@ -28,8 +28,10 @@
<!-- <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>
<!-- <class>com.hello.jpa.inheritance.ex01.Member</class>-->
<!-- <class>com.hello.jpa.inheritance.ex01.Team</class>-->
<class>com.hello.jpa.proxy.ex00.Member</class>
<class>com.hello.jpa.proxy.ex00.Team</class>
<properties>
<!-- 필수 속성 -->