티스토리 뷰

728x90
반응형

지연 로딩

  •  예시
    • Member 와 team이 있을 경우 Member만 조회를 하고자 할 경우
    • Member와 team은 다 대 1관계를 가지고 있다.
    • 이럴 경우 지연 로딩을 이용한다.
    • fetch = FetchType.LAZY를 선언하여 사용
@Entity
 public class Member {
 @Id
 @GeneratedValue
 private Long id;
 @Column(name = "USERNAME")
 private String name;
 @ManyToOne(fetch = FetchType.LAZY) // 관계를 가지는 대상에 fetch 선언
 @JoinColumn(name = "TEAM_ID")
 private Team team;
 .. 
 }
  • 지연 로딩시 프록시 개념이 사용된다.
    • 프록시와 같이 가짜 객체만 들고 온 상황에서 필요할 때 조회를 하는 방식을 가진다.

지연 로딩일 경우 코드 예시
Member member = em.find(Member.class,"member1");
System.out.println("member : "+member.getName()); // member 조회시 member 만 가져옴
//---------------------------------------------//
//team을 사용하고자 할 시 프록시 조회를 통해 db에 team만 조회에옴
System.out.println("team : "+member.getTeam().getName()); 

결과: 
select
        member0_.MEMBER_ID as member_i1_1_0_,
        member0_.createBy as createby2_1_0_,
        member0_.createDate as createda3_1_0_,
        member0_.modifyDate as modifyda4_1_0_,
        member0_.modifyedBy as modifyed5_1_0_,
        member0_.city as city6_1_0_,
        member0_.name as name7_1_0_,
        member0_.street as street8_1_0_,
        member0_.TEAM_ID as team_id10_1_0_,
        member0_.zipcode as zipcode9_1_0_ 
    from
        Member member0_ 
    where
        member0_.MEMBER_ID=?

		member : member1
--------------------------------------------------
Hibernate: 
    select
        team0_.id as id1_2_0_,
        team0_.name as name2_2_0_ 
    from
        Team team0_ 
    where
        team0_.id=?

		team  : team1

즉시로딩

Member와 Team을 같이 사용하는 경우
  • fetch = FetchType.EAGER를 선언하여 사용
@Entity
 public class Member {
 @Id
 @GeneratedValue
 private Long id;
 @Column(name = "USERNAME")
 private String name;
 @ManyToOne(fetch = FetchType.EAGER) //Member 실행시 Team도 즉시 조회됨
 @JoinColumn(name = "TEAM_ID")
 private Team team;
 .. 
 }



Member member = em.find(Member.class,"member1");//member 와 team을 join 하여 같이 조회
System.out.println("member : "+member.getName());
System.out.println("team : "+member.getTeam().getName()); 

결과 : 
select
        member0_.MEMBER_ID as member_i1_1_0_,
        member0_.createBy as createby2_1_0_,
        member0_.createDate as createda3_1_0_,
        member0_.modifyDate as modifyda4_1_0_,
        member0_.modifyedBy as modifyed5_1_0_,
        member0_.city as city6_1_0_,
        member0_.name as name7_1_0_,
        member0_.street as street8_1_0_,
        member0_.TEAM_ID as team_id10_1_0_,
        member0_.zipcode as zipcode9_1_0_,
        team1_.id as id1_2_1_,
        team1_.name as name2_2_1_ 
    from
        Member member0_ 
    left outer join
        Team team1_ 
            on member0_.TEAM_ID=team1_.id 
    where
        member0_.MEMBER_ID=?

member : member1
team : team1

프록시와 즉시로딩 주의 

프록시 관한 내용은 https://ghgo195.tistory.com/10 을 참조
  • 가급적 지연 로딩만 사용
  • 즉시 로딩을 적용하면 예상하지 못한 SQL이 발생
  • 즉시 로딩은 JPQL에서 N+1 문제를 일으킨다.
  • @ManyToOne, @OneToOne은 기본이 즉시 로딩 → Lazy로 설정
  • @OneToMany, @ManyToMany 는 기본 설정이 Lazy(지연로딩)
728x90
반응형

'프로그램 언어 > Spring JPA' 카테고리의 다른 글

기본값 타입 (1)  (0) 2023.04.30
영속성 전이(CASCADE)  (0) 2023.04.28
PROXY(프록시)  (0) 2023.04.28
@MappedSuperclass(공통 속성 매핑)  (0) 2023.04.27
SPRING JPA 상속 관계 매핑 (조인 전략)  (0) 2023.04.27
250x250
반응형
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
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
글 보관함