Skip to content

Commit

Permalink
[Week9][Chap4] Feat : 엔티티 그래프(#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
songhee1 committed Nov 21, 2023
1 parent e42d0cc commit 397485d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -37,4 +38,8 @@ public interface MemberRepository extends JpaRepository<Member, Long> {
@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<Member> findAll();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Member> members = memberRepository.findAll();
for (Member member : members) {
System.out.println("member = "+member.getUsername());
System.out.println("member.team = "+member.getTeam().getName());
}

}
}

0 comments on commit 397485d

Please sign in to comment.