Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Android
- springdatajpa
- SpringBoot
- 스프링 핵심 원리
- Proxy
- pointcut
- 백준
- java
- db
- JPQL
- 스프링 핵심 기능
- 그리디
- transaction
- spring
- QueryDSL
- Exception
- Greedy
- Spring Boot
- AOP
- kotlin
- 인프런
- 김영한
- Servlet
- Thymeleaf
- http
- JDBC
- 스프링
- 알고리즘
- 자바
- jpa
Archives
- Today
- Total
개발자되기 프로젝트
예제 도메인 모델과 동작 확인하기. 본문
1. Entity Class
- Member와 Team은 다대일 연관관계
2. ERD
3. Member class
- xToOne은 기본적으로 fetch type이 EAGER이다.
- 항상 LAZY를 적용하자(지연로딩)
package study.datajpa.entity;
import lombok.*;
import javax.persistence.*;
@Entity
@Getter @Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@ToString(of = {"id", "userName", "age"})
public class Member {
@Id @GeneratedValue
@Column(name = "member_id")
private Long id;
private String userName;
private int age;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "team_id")
private Team team;
public Member(String userName) {
this.userName = userName;
}
public Member(String userName, int age, Team team) {
this.userName = userName;
this.age = age;
if(team!=null) {
this.changeTeam(team);
}
}
public void changeUserName(String userName){
this.userName = userName;
}
public void changeTeam(Team team){
this.setTeam(team);
team.getMembers().add(this);
}
}
4. Team Class
package study.datajpa.entity;
import lombok.*;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
@Getter
@Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@ToString(of = {"id", "name"})
public class Team {
@Id @GeneratedValue
@Column(name = "team_id")
private Long id;
private String name;
@OneToMany(mappedBy = "team")
private List<Member> members = new ArrayList<>();
public Team(String name) {
this.name = name;
}
}
5. Test
- 지연 로딩을 적용하면 member.getTeam()실행 시
- team 프록시 객체가 초기화 되며 team을 조회하는 쿼리가 나간다.
- 따라서 member를 조회하는 쿼리로 인해 각member에서 team을 조회할 때 쿼리가 매번 나간다.
- fetch join을 사용하면 쿼리 한번에 member와 관련된 team을 모두 한 번에 가져옴.
@SpringBootTest
@Transactional
class MemberTest {
@PersistenceContext
private EntityManager em;
@Test
public void testEntity(){
Team teamA = new Team("teamA");
Team teamB = new Team("teamB");
em.persist(teamA);
em.persist(teamB);
Member member1 = new Member("member1", 10, teamA);
Member member2 = new Member("member2", 20, teamA);
Member member3 = new Member("member3", 30, teamB);
Member member4 = new Member("member4", 40, teamB);
em.persist(member1);
em.persist(member2);
em.persist(member3);
em.persist(member4);
em.flush();
em.clear();
List<Member> members = em.createQuery(
"select m from Member m join fetch m.team", Member.class)
.getResultList();
for (Member member : members) {
System.out.println("member = " + member);
System.out.println("member.getTeam() = " + member.getTeam());
}
}
}
6. Result
select
member0_.member_id as member_i1_0_0_,
team1_.team_id as team_id1_1_1_,
member0_.age as age2_0_0_,
member0_.team_id as team_id4_0_0_,
member0_.user_name as user_nam3_0_0_,
team1_.name as name2_1_1_
from
member member0_
inner join
team team1_
on member0_.team_id=team1_.team_id
member = Member(id=3, userName=member1, age=10)
member.getTeam() = Team(id=1, name=teamA)
member = Member(id=4, userName=member2, age=20)
member.getTeam() = Team(id=1, name=teamA)
member = Member(id=5, userName=member3, age=30)
member.getTeam() = Team(id=2, name=teamB)
member = Member(id=6, userName=member4, age=40)
member.getTeam() = Team(id=2, name=teamB)
7. GitHub : 210827 Domain model
'인프런 > [인프런] Spring Data JPA' 카테고리의 다른 글
JPA NamedQuery (0) | 2021.08.28 |
---|---|
쿼리 메서드 (0) | 2021.08.28 |
공통 인터페이스 설정, 적용, 분석 (0) | 2021.08.28 |
공통 인터페이스 기능 : 순수 jpa기반 repository (0) | 2021.08.28 |
세팅하기 (0) | 2021.08.27 |
Comments