String jpql = "select m from Member m";
List<Member> members = em.createQuery(jpql, Member.class).getResultList();
for (Member member : members) {
System.out.println("username = " + member.getUsername() + ", " +
"teamName = " + member.getTeam().name());
//회원1, 팀A(SQL)
//회원2, 팀A(1차 캐시)
//회원3, 팀B(SQL)
//회원 100명 -> N + 1
}
최대 N + 1번 Team 조회 쿼리 수행
- fetch join
String jpql = "select m from Member m join fetch m.team";
List<Member> members = em.createQuery(jpql, Member.class).getResultList();
for (Member member : members) {
//페치 조인으로 회원과 팀을 함께 조회해서 지연 로딩X
System.out.println("username = " + member.getUsername() + ", " +
"teamName = " + member.getTeam().name());
}
일대다를 다대일로 방향을 전환하여 해결한다
- BatchSize()
public class Team{
...
@BatchSize(size = 100)
@OneToMany(mappedBy = "team")
private List<Member> members;
...
}
String query = "select t from Team t";
- 출처
김영한님 인프런 강의 중 한 곳
[패키징및배포] Jar vs War (0) | 2024.12.21 |
---|---|
[Spring] @Transactional (0) | 2024.12.13 |
[스프링] AOP와 @Transactional (0) | 2024.11.27 |
[스프링] 동시성 문제, ThreadLocal (0) | 2024.11.25 |
[Spring] Spring IoC Container, BeanScan, 생명주기, 스코프 (0) | 2024.11.20 |