• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

team 조회시 query 관련

21.07.30 05:46 작성 조회수 157

0

영한님 log와 달리,

제가 조회를 했을때는 hibernate가 for문에서 해당 팀을 조회 하는 순서에 따라 select쿼리를 날려서

데이터를 db에서 가져온 후 print해주는데요

영한님 로그에서는 미리 team1,2 를 다 조회하고 그 다음에 출력해주는것으로 보입니다.

(여기서 이미 조회해서 영속성 컨텐츠로 만든 것들은 다시 select 쿼리를 날리진 알고요

team 1, 2 의 경우 1번씩만 날림) em.clear해서 캐시를 초기화 했기 때문에,

getTeam을 하였을 때,
아래의 결과 처럼 getTeam으로 조회 하려고 할때, hibernate가 select 쿼리를 날리는게 맞다고 보는데


영한님 로그에서는 어떻게 hibernate가 미리 2개를 다 조회할 것을 알고 select문을 먼저 2번 날린 것인지 궁금합니다.

System.out.println("============");
for(Member member:members){
System.out.println(member);
System.out.println(member.getTeam());
}
System.out.println("============");
============
Member(id=3, username=member1, age=10)
2021-07-30 05:38:40.449 DEBUG 1508 --- [           main] org.hibernate.SQL                        : select team0_.team_id as team_id1_1_0_, team0_.name as name2_1_0_ from team team0_ where team0_.team_id=?
2021-07-30 05:38:40.451  INFO 1508 --- [           main] p6spy                                    : #1627591120451 | took 0ms | statement | connection 3| url jdbc:h2:tcp://localhost/~/lecture_datajpa
select team0_.team_id as team_id1_1_0_, team0_.name as name2_1_0_ from team team0_ where team0_.team_id=?
select team0_.team_id as team_id1_1_0_, team0_.name as name2_1_0_ from team team0_ where team0_.team_id=1;
Team(id=1, name=teamA)
Member(id=4, username=member2, age=10)
Team(id=1, name=teamA)
Member(id=5, username=member3, age=10)
2021-07-30 05:38:40.460 DEBUG 1508 --- [           main] org.hibernate.SQL                        : select team0_.team_id as team_id1_1_0_, team0_.name as name2_1_0_ from team team0_ where team0_.team_id=?
2021-07-30 05:38:40.461  INFO 1508 --- [           main] p6spy                                    : #1627591120461 | took 0ms | statement | connection 3| url jdbc:h2:tcp://localhost/~/lecture_datajpa
select team0_.team_id as team_id1_1_0_, team0_.name as name2_1_0_ from team team0_ where team0_.team_id=?
select team0_.team_id as team_id1_1_0_, team0_.name as name2_1_0_ from team team0_ where team0_.team_id=2;
Team(id=2, name=teamB)
Member(id=6, username=member4, age=10)
Team(id=2, name=teamB)
============

답변 3

·

답변을 작성해보세요.

0

안녕하세요. 승현님

해당 궁금증은 JPA 기본편에서 자세히 알려드립니다.

감사합니다.

0

유승현님의 프로필

유승현

질문자

2021.07.30

그렇다면 추가적으로 궁금한 것은,

실무에서 어떤 상황에서는 eager로 받아야 하는  case도 있을 것같은데,

어떨때 eager처리하는 지 궁금합니다.

0

유승현님의 프로필

유승현

질문자

2021.07.30

아 제가 미리 fetct lazy 옵션을 줘서 그런거였네요 확인했습니다.