객체를 테이블에 맞추어 데이터 중심으로 모델링하면, 협력 관계를 만들 수 없다.
@Entity
public class Member {
@Id @GeneratedValue
@Column(name = "MEMBER_ID")
private Long id;
@Column(name = "USERNAME")
private String name;
@Column(name = "TEAM_ID")
private Long teamId;
}
@Entity
public class Team {
@Id @GeneratedValue
@Column(name = "TEAM_ID")
private Long id;
private String name;
}
//저장
Team team = new Team();
team.setName("TeamA");
em.persist(team);
Member member = new Member();
member.setUsername("member1");
member.setTeamId(team.getId());
em.persist(member);
// 연관관계 설정을 안해주면 객체지향스럽지 않음.
Member findMember = em.find(Member.class, member.getId());
Long findTeamId = findMember.getTeadId();
Team findTeam = em.find(Team.class, findTeamId);
객체의 참조와 테이블의 외래 키를 매핑(Member -> Team)
@Entity
public class Member {
@Id @GeneratedValue
@Column(name = "MEMBER_ID")
private Long id;
@Column(name = "USERNAME")
private String name;
@ManyToOne // Member가 Many, Team이 One
@JoinColumn(name = "TEAM_ID")
private Team team;
}
@Entity
public class Team {
@Id @GeneratedValue
@Column(name = "TEAM_ID")
private Long id;
private String name;
}
Team team = new Team();
team.setName("TeamA");
em.persist(team);
Member member = new Member();
member.setUsername("member1");
member.setTeam(team); //단방향 연관관계 설정, 참조 저장
em.persist(member);
Member findMember = em.find(Member.class, member.getId());
Team findTeam = findMember.getTeam(); //참조를 사용해서 연관관계 조회
단방향 연관관계에서는 한쪽 방향(Member->Team)으로밖에 갈 수 없었지만 양방향은 양쪽으로 가능(Member<->Team)
@Entity
public class Member {
@Id @GeneratedValue
@Column(name = "MEMBER_ID")
private Long id;
@Column(name = "USERNAME")
private String name;
@ManyToOne // Member가 Many, Team이 One
@JoinColumn(name = "TEAM_ID")
private Team team;
}
@Entity
public class Team {
@Id @GeneratedValue
@Column(name = "TEAM_ID")
private Long id;
private String name;
@OneToMany(mappedBy = "team") // 양방향
private List<Member> members = new ArrayList<>();
}
Team team = new Team();
team.setName("TeamA");
em.persist(team);
Member member = new Member();
member.setUsername("member1");
member.setTeam(team);
em.persist(member);
Member findMember = em.find(Member.class, member.getId());
List<Member> members = findMember.getTeam().getMembers();
for(Member m : members) {
System.out.println(m.getUserName());
}
객체 연관관계 = 2개 : 참조가 양쪽에 있어야함
1) 회원 -> 팀 연관관계 1개(단방향)
2) 팀 -> 회원 연관관계 1개(단방향)
테이블 연관관계 = 1개 : TEAM_ID(FK)를 기준으로 관계가 맺어짐
1) 회원 <-> 팀의 연관관계 1개(양방향)
class A {
B b;
}
class B {
A a;
}
member.setTeam(team);
을 추가해줘야 함!Team team = new Team();
team.setName("TeamA");
em.persist(team);
Member member = new Member();
member.setName("member1");
em.persist(member);
//역방향(주인이 아닌 방향)만 연관관계 설정. Member.Team이 null이 되어버림.
team.getMembers().add(member);
Team team = new Team();
team.setName("TeamA");
em.persist(team);
Member member = new Member();
member.setName("member1");
em.persist(member);
//team.getMembers().add(member);
//em.flush();
//em.clear();
Team findTeam = em.find(Team.class, team.getId());
List<Member> members = findTeam.getMembers();
for(Member m : members) {
System.out.println(m);
}
Member 엔티티에서 Team 값 세팅 시 양쪽에 설정
public class Member {
public void setTeam(Team team) {
this.team = team; // 단방향
team.getMembers.add(this); // 역방향
}
}
Team 엔티티에서 해줘도 된다.(둘 중 하나만)
public class Team {
public void addMember(Member member) {
members.add(member);
member.setTeam(this);
}
}