11. Configuring Fetch- and LoadGraphs
Entity Graph
- 엔티티 조회 시, 연관 엔티티들을 fetch 하기 위한 구성 (어떤 연관 엔티티를 함께 로딩할 지 정의)
특징 | Fetch Graph | Load Graph |
정의 | 명시된 속성만 Fetch하고 나머지 속성은 Lazy로 처리 |
명시된 속성만 Fetch하고 나머지 속성은 FetchType 설정에 따름
|
사용법 | javax.persistence.fetchgraph를 사용 |
javax.persistence.loadgraph를 사용
|
사용 시점 | 엔티티를 조회할 때 실행 시점에 따라 로딩될 속성 지정 |
엔티티를 조회할 때 그래프에 따라 로딩될 속성 지정
|
예제) 엔티티 그래프 정의
더보기
Entity
@Entity
@NamedEntityGraph(name = "GroupInfo.detail", attributeNodes = @NamedAttributeNode("members"))
public class GroupInfo {
@ManyToMany // 기본 페치 모드는 lazy
List<GroupMember> members = new ArrayList<GroupMember>();
}
- @NamedEntityGraph: Entity Graph 정의 (entity)
- @NamedAttributeNode: entity graph 속성 정의
repository
public interface GroupRepository extends CrudRepository<GroupInfo, String> {
@EntityGraph(value = "GroupInfo.detail", type = EntityGraphType.LOAD)
GroupInfo getByGroupName(String name);
}
- @EntityGraph: 엔티티 그래프 참조
- value: 참조할 Entity Graph
- type: Entity Graph Type
예제) 엔티티 그래프 사용
더보기
힌트값으로 결정됨
TypedQuery<GroupInfo> query = entityManager.createQuery("SELECT g FROM GroupInfo g", GroupInfo.class);
query.setHint("javax.persistence.fetchgraph", entityManager.getEntityGraph("GroupInfo.detail"));
List<GroupInfo> result = query.getResultList();
TypedQuery<GroupInfo> query = entityManager.createQuery("SELECT g FROM GroupInfo g", GroupInfo.class);
query.setHint("javax.persistence.loadgraph", entityManager.getEntityGraph("GroupInfo.detail"));
List<GroupInfo> result = query.getResultList();
출처
'Spring > Spring Data JPA' 카테고리의 다른 글
[Spring Data JPA] 3-3. Custom Repository Implementations (0) | 2024.08.02 |
---|---|
[Spring Data JPA] 3-2. JPA Query Methods (4) (0) | 2024.08.02 |
[Spring Data JPA] 3-2. JPA Query Methods (3) (0) | 2024.08.02 |
[Spring Data JPA] 3-2. JPA Query Methods (5) (0) | 2024.08.02 |
[Spring Data JPA] 3-2. JPA Query Methods (2) (0) | 2024.08.02 |