작성
·
90
·
수정됨
0
[질문 템플릿]
1. 강의 내용과 관련된 질문인가요? (예/아니오) 네
2. 인프런의 질문 게시판과 자주 하는 질문에 없는 내용인가요? (예/아니오) 네
3. 질문 잘하기 메뉴얼을 읽어보셨나요? (예/아니오)
[질문 내용]
여기에 질문 내용을 남겨주세요.
member = Member(id=1, username=member1, age=10)
member.getTeam() = Team(id=1, name=teamA)
memberList = [Member(id=1, username=member1, age=10), Member(id=2, username=member2, age=20)]
member = Member(id=2, username=member2, age=20)
member.getTeam() = Team(id=1, name=teamA)
memberList = [Member(id=1, username=member1, age=10), Member(id=2, username=member2, age=20)]
member = Member(id=3, username=member3, age=30)
member.getTeam() = Team(id=1, name=teamA)
memberList = [Member(id=3, username=member3, age=30), Member(id=4, username=member4, age=40)]
member = Member(id=4, username=member4, age=40)
member.getTeam() = Team(id=1, name=teamA)
memberList = [Member(id=3, username=member3, age=30), Member(id=4, username=member4, age=40)]
이부분이 soutv 프린트결과입니다.
이부분부터 테스트코드인데요 여기서분명히 팀을 다르게넣어주었고 실제로 db에도 적용되어있습니다.
@Test
@DisplayName("entity test")
void entityTest() {
Team team = new Team("teamA");
Team teamB = new Team("teamB");
em.persist(team);
em.persist(teamB);
Member member = new Member("member1",10,team);
Member member1 = new Member("member2",20,team);
Member member2 = new Member("member3",30,teamB);
Member member3 = new Member("member4",40,teamB);
em.persist(member);
em.persist(member1);
em.persist(member2);
em.persist(member3);
List<Member> members = em.createQuery("select m from Member m", Member.class).getResultList();
for (Member memberList : members) {
System.out.println("member = " + memberList);
System.out.println("member.getTeam() = " + member.getTeam());
System.out.println("memberList = " + memberList.getTeam().getMembers());
}
Assertions.assertThat(member.getTeam()).isEqualTo(team);
Assertions.assertThat(member1.getTeam()).isEqualTo(team);
Assertions.assertThat(member2.getTeam()).isEqualTo(teamB);
Assertions.assertThat(member3.getTeam()).isEqualTo(teamB);
}
어떤게 문제인지모르겠어서 질문남깁니다!
entity code 도 남기겠습니다.
package study.datajpa.entity;
import jakarta.persistence.*;
import lombok.*;
@Entity
@Getter
@Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@ToString(of = {"id","username","age"})
public class Member {
@Id
@GeneratedValue
@Column(name = "member_id")
private Long id;
private String username;
private int age;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "team_id")
private Team team;
public Member(String username) {
this.username = username;
}
public void memberTeamSetting(Team team) {
if (this.team != null) {
this.team.getMembers().remove(this);
}
this.team = team;
team.getMembers().add(this);
}
public Member(String username, int age, Team team) {
this.username = username;
this.age = age;
if(team != null) {
memberTeamSetting(team);
}
}
}
package study.datajpa.entity;
import jakarta.persistence.*;
import lombok.*;
import java.util.ArrayList;
import java.util.List;
@Entity
@Getter
@Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@ToString(of = {"id","name"})
public class Team {
@Id @GeneratedValue
@Column(name = "team_id")
private Long id;
private String name;
@OneToMany(mappedBy = "team")
private List<Member> members = new ArrayList<>();
public Team(String name) {
this.name = name;
}
}
답변 1
0
안녕하세요. 김준영님, 공식 서포터즈 David입니다.
List<Member> members = em.createQuery("select m from Member m", Member.class).getResultList();
for (Member memberList : members) {
System.out.println("member = " + memberList);
System.out.println("member.getTeam() = " + member.getTeam());
System.out.println("memberList = " + memberList.getTeam().getMembers());
}
위와 같이 작성해 주셨는데, memer.getTeam()이 아니라 memberList.getTeam()을 해주셔야 합니다.
감사합니다.