๋ฐ์ํ
๋ค๋๋ค ๋งคํ์ ์ ๋งํ๋ฉด ์์ฐ๋๊ฒ ์ต์ ์ด๋ค.
๋ค๋๋ค ๋งคํ ๋ฐฉ๋ฒ์ด ๋๊ฐ์ง๊ฐ ์๋๋ฐ
- @ManyToMany๋ฅผ ์ฌ์ฉํ๋๊ฒ์ด๋ค.
- ์ฐ๊ฒฐ ํ ์ด๋ธ์ฉ ์ํฐํฐ ์ถ๊ฐํด์ @OneToMany๋ฅผ ์ฌ์ฉ (์ฐ๊ฒฐํ ์ด๋ธ์ ์ํฐํฐ๋ก ์น๊ฒฉ)
โ ๋ค๋๋ค [N:M]
1. @ManyToMany ๋ฅผ ์ฌ์ฉ
MEMBER_PRODUCT ๋ผ๋ ์ค๊ฐ ํ ์ด๋ธ์ด ์์ฑ์ด ๋๋ค.
@Entity
public class Member {
@ManyToMany
@JoinTable(name = "MEMBER_PRODUCT") // ์ด๋ ๊ฒ ํ๋ฉด MEMBER_PRODUCT๋ผ๋ ํ
์ด๋ธ์ด ์์ฑ๋๋ค.
private List<Product> products = new ArrayList<>();
// ๋ค๋ฅธ ๋ฉ์๋ ์๋ต
}
@Entity
public class Product {
@ManyToMany(mappedBy = "products")
private List<Member> members = new ArrayList<>();
// ๋ค๋ฅธ ๋ฉ์๋ ์๋ต
}
2. @OneToMany , ์ฐ๊ฒฐ ํ ์ด๋ธ์ฉ ์ํฐํฐ ์ถ๊ฐ
@Entity
public class Member_Product {
@Id @GeneratedValue
private Long id;
...
@ManyToOne
@JoinColumn(name = "MEMBER_ID")
private Member member;
@ManyToOne
@JoinColumn(name = "PRODUCT_ID")
priavet Product product;
// ๋ค๋ฅธ ๋ฉ์๋ ์๋ต
}
@Entity
public class Member {
@OneToMany(mappedBy = "members")
private List<Member_Product> products = new ArrayList<>();
// ๋ค๋ฅธ ๋ฉ์๋ ์๋ต
}
@Entity
public class Product {
@OneToMany(mappedBy = "product")
private List<Member_Product> member_products = new ArrayList<>();
// ๋ค๋ฅธ ๋ฉ์๋ ์๋ต
}
๋ฐ์ํ