인프런 커뮤니티 질문&답변

jaewkim님의 프로필 이미지

작성한 질문수

자바 ORM 표준 JPA 프로그래밍 - 기본편

페치 조인 1 - 기본

JOIN FETCH와 1차 캐시 질문드립니다.

23.04.27 21:00 작성

·

481

0

JOIN FETCH를 이용하여 쿼리를 작성하고 테스트 해보던 중에 이해가지 않는 동작이 발견되어 질문드려요!

JOIN FETCH로 채우려는 필드가, 1차 캐시에 엔티티가 이미 존재할 때에, 해당 필드가 null 이 되는 현상입니다.

오히려 영속성 컨텍스트를 비워주면 프록시를 가져오는 것 같습니다... 어떻게 동작하는 건지 질문 드립니다!


테스트 메서드를 간단하게 작성하며 생성 및 조회를 하나의 트랜잭션에서 진행했습니다.

엔티티는 간략하게 다음과 같습니다.

@Entity

public class Collection {

  @Id
  @GeneratedValue
  private Long id;

  @OneToMany(fetch = FetchType.LAZY, mappedBy = "collection", cascade = CascadeType.ALL, orphanRemoval = true)
  private List<ReviewInCollection> reviews = new ArrayList<>();
   
  // ...
}

@Entity
public class ReviewInCollection {

  @Id
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "COLL_ID")
  private Collection collection;

  @Id
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "REV_ID")
  private Review review;

  // ...
}

@Entity
public class Review {

  @Id
  @GeneratedValue
  @Column(name = "REV_ID")
  private Long id;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "PLACE_ID", nullable = false)
  private Place place;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "MEMBER_ID", nullable = false)
  private Member member;
 
  // ...
}

 

문제가 발생한 코드입니다.

  @Test
  void updateReviewsTest() {
    final SessionImplementor session = em.unwrap(SessionImplementor.class);
    final PersistenceContext pc = session.getPersistenceContext();

    // mock up

    //!=========================================================================================!//

    em.flush();
    // em.clear();
    Collection coll = em.createQuery(
                            "select c from Collection c " 
                            + "join fetch c.reviews cr " 
                            + "join fetch cr.review r where c.id = :collId ",
                            Collection.class)
                        .setParameter("collId", 1L)
                        .getSingleResult();

    coll.getReviews() // ReviewInCollection
        .stream()
        .map(cr -> cr.getReview() // Review → NPE
                     .getId()
             )
        .toList();
   }
Cannot invoke "com._2cha.demo.review.domain.Review.getId()" because the return value of "com._2cha.demo.collection.domain.ReviewInCollection.getReview()" is null
java.lang.NullPointerException: Cannot invoke "com._2cha.demo.review.domain.Review.getId()" because the return value of "com._2cha.demo.collection.domain.ReviewInCollection.getReview()" is null

JOIN FETCH 를 통해

Collection.reviews
ReviewInCollection.review

를 채워 오고자 했는데,
Collection → for each ReveiwInCollection → review 가 null 로 채워지게 됩니다.


> 쿼리 이전의 영속성 컨텍스트입니다.


> 쿼리 이후 영속성 컨텍스트입니다.

 

주석 처리한 em.clear() 를 실행하면,  null이 아닌 프록시로 가져와서 LAZY 방식으로 Review를 채웁니다.

LAZY 또한 예상한 동작이 아니긴 하나, 일단 왜 null이 들어가는지가 가장 궁금합니다... 영속성 컨텍스트와 JOIN FETCH의 충돌이 있는 걸까요?

답변 1

0

김영한님의 프로필 이미지
김영한
지식공유자

2023. 04. 30. 20:02

안녕하세요. jaewkim님

전체 프로젝트를 압축해서 구글 드라이브로 공유해서 링크를 남겨주세요.

구글 드라이브 업로드 방법은 다음을 참고해주세요.

https://bit.ly/3fX6ygx

주의: 업로드시 링크에 있는 권한 문제 꼭 확인해주세요

추가로 다음 내용도 코멘트 부탁드립니다.

1. 실행 방법을 알려주세요.

2. 어떻게 문제를 확인할 수 있는지 자세한 설명을 남겨주세요.

감사합니다.

 

jaewkim님의 프로필 이미지

작성한 질문수

질문하기