class Team {
@OneToMany(mappedBy="team") // 연관관계의 주인은 Member.team
private List<Member> members = new ArrayList<Member>();
}
Member member1 = new Member("member1", "회원1"); // Member가 주인
em.persist(member1)
Team team1 = new Team("team1", "팀1");
team1.getMembers().add(member1); // 주인이 아닌 곳(Team)에만 연관관계를 설정했다.
member1.setTeam(team1); // 연관관계 설정 member1 -> team1
team1.getMembers().add(member1); // 연관관계 설정 team1 -> member1
// Member
private Team team;
public void setTeam(Team team) {
this.team = team;
team.getMembers().add(this);
}
// 연관관계 매핑
@ManyToOne
@JoinColumn(name="TEAM_ID")
private Team team;
// 연관관계 설정
public void setTeam(Team team) {
this.team = team;
}
// Member
@ManyToOne
@JoinColumn(name = "TEAM_ID")
private Team team;
// Team
@OneToMany(mappedBy = "team")
private List<Member> members = new ArrayList<Member>();
// Member
@ManyToOne
@JoinColumn(name = "TEAM_ID", insertable = false, updatable = false)
private Team team; // Member.team 은 읽기 전용이 되었다
// Memebr
@OneToOne
@JoinColumn(name = "LOCKER_ID")
private Locker locker;
// Memebr
@OneToOne
@JoinColumn(name = "LOCKER_ID")
private Locker locker;
// Locker
@OneToOne(mappedBy = "locker")
private Member member;
Member
, Product
두 엔티티가 다대다로 매핑될 경우, 데이터베이스에는 Member_Product
가 생긴다.[참고자료]
<자바 ORM 표준 JPA 프로그래밍>