인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

Inflearn Community Q&A

tommy7899's profile image
tommy7899

asked

Java ORM Standard JPA Programming - Basics

Practical Example 2 - Association Mapping Start

질문있습니다!

Written on

·

307

0

Team team = em.find(Team.class, "team1");
List<Member> members = team.getMembers();
for (Member member:members){
           System.out.println("member.getName() = " + member.getName());
  }

지금 현재 제 데이터 베이스에 team1으로 등록된 member들이 3명이 있는데 갑자기 궁금한게 생겼습니다.

Team team = em.find(Team.class, "team1") --> 이 부분에서 이 한 줄 코드만으로 team1에 해당하는 모든 멤버들을 찾아와서 변수 team에 담기는건가요? 그래서 다음 줄 코드에서 team.getMembers()를 할 수 있는건가요?

 

 

JPAjava

Answer 2

1

안녕하세요. tommy7899님, 공식 서포터즈 OMG입니다.

양방향 연관관계와 연관관계의 주인 2 - 주의점, 정리 강의의 16분 58초와 같이 

find하기 전,  team과 member가 연관관계가 맺어져 있을 시에 조회가 가능합니다.

 

그런데 작성하신 코드에서

Team team = em.find(Team.class, "team1");

 

두번째 인자가 "team1" 인것으로 보아 id부분을 강의와 다르게 작성하신 것으로 보이네요.

 

감사합니다.

0

tommy7899님의 프로필 이미지
tommy7899
Questioner

예를 들어 team.getId()의 결과값이 teamA라고 한다면 DB에 현재 teamA에 해당되는 멤버가 3명이 있다고 한다면

Team findTeam = em.find(Team.class, team.getId());

이렇게 했을 때 findTeam에 이 멤버 3명의 객체가 담기는건가요?

 

네 맞습니다

tommy7899님의 프로필 이미지
tommy7899
Questioner

감사합니다!

tommy7899's profile image
tommy7899

asked

Ask a question