diff --git "a/\354\265\234\354\206\241\355\235\254/data-jpa/src/main/java/study/datajpa/repository/MemberRepository.java" "b/\354\265\234\354\206\241\355\235\254/data-jpa/src/main/java/study/datajpa/repository/MemberRepository.java" index 716396aa..5693d27f 100644 --- "a/\354\265\234\354\206\241\355\235\254/data-jpa/src/main/java/study/datajpa/repository/MemberRepository.java" +++ "b/\354\265\234\354\206\241\355\235\254/data-jpa/src/main/java/study/datajpa/repository/MemberRepository.java" @@ -2,6 +2,7 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.EntityGraph; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; @@ -37,4 +38,8 @@ public interface MemberRepository extends JpaRepository { @Modifying(clearAutomatically = true) @Query("update Member m set m.age = m.age+1 where m.age>= :age") int bulkAgePlus(@Param("age")int age); + + @Override + @EntityGraph(attributePaths = {"team"}) + List findAll(); } diff --git "a/\354\265\234\354\206\241\355\235\254/data-jpa/src/test/java/study/datajpa/repository/MemberRepositoryTest.java" "b/\354\265\234\354\206\241\355\235\254/data-jpa/src/test/java/study/datajpa/repository/MemberRepositoryTest.java" index 7d808d03..55260632 100644 --- "a/\354\265\234\354\206\241\355\235\254/data-jpa/src/test/java/study/datajpa/repository/MemberRepositoryTest.java" +++ "b/\354\265\234\354\206\241\355\235\254/data-jpa/src/test/java/study/datajpa/repository/MemberRepositoryTest.java" @@ -191,4 +191,31 @@ public void bulkUpdate(){ assertThat(resultCount).isEqualTo(3); } + + @Test + public void findMemberLazy(){ + //given + //member1->teamA참조 + //member2->teamB참조 + + Team teamA = new Team("teamA"); + Team teamB= new Team("teamB"); + teamRepository.save(teamA); + teamRepository.save(teamB); + + Member member1 = new Member("member1", 10, teamA); + Member member2 = new Member("member2", 20, teamB); + memberRepository.save(member1); + memberRepository.save(member2); + + entityManager.flush(); + entityManager.clear(); + + List members = memberRepository.findAll(); + for (Member member : members) { + System.out.println("member = "+member.getUsername()); + System.out.println("member.team = "+member.getTeam().getName()); + } + + } } \ No newline at end of file