이 글은 김영한의 [자바 ORM 표준 JPA 프로그래밍 - 기본편]을 수강하며 정리한 글입니다.
👉 기본 환경
- Language: Java
- DB: H2 Database
- IDE: IntelliJ
다대다 매핑
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
public class Member {
@Id @GeneratedValue
@Column(name = "MEMBER_ID")
private Long id;
@Column(name = "USER_NAME")
private String name;
@ManyToMany
@JoinTable(name = "MEMBER_PRODUCT")
private List<Product> products = new ArrayList<>();
}
|
Member Entity
- 연관 관계 주인
- JoinTable 생성
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
public class Product {
@Id @GeneratedValue
@Column(name = "PRODUCT_ID")
private Long id;
@Column(name = "PRODUCT_NAME")
private String name;
@ManyToMany(mappedBy = "products")
private List<Member> members = new ArrayList<>();
}
|
Product Entity
- 단순 참조를 위한 매핑
⭐ 관계형 데이터베이스에서 다대다 (Many-to-Many) 관계를 직접적으로 표현하는 것은 불가능
▶ 연결 테이블을 추가해서 일대다, 다대일 관계로 풀어내야 함
⭐ 중간 테이블에 새로운 컬럼을 추가하는 게 어려워 유연성이 떨어짐
- @JoinTable을 통해서 PK, FK 관계 이외의 필드를 설정하기 어려움
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
Hibernate:
create table Member (
MEMBER_ID bigint not null,
USER_NAME varchar(255),
primary key (MEMBER_ID)
)
Hibernate:
create table MEMBER_PRODUCT (
members_MEMBER_ID bigint not null,
products_PRODUCT_ID bigint not null
)
Hibernate:
create table Product (
PRODUCT_ID bigint not null,
PRODUCT_NAME varchar(255),
primary key (PRODUCT_ID)
)
Hibernate:
alter table Member
add constraint FK332130jlg9s5hyeuk7gfgi052
foreign key (LOCKER_ID)
references Locker
Hibernate:
alter table MEMBER_PRODUCT
add constraint FKfmfxdrleengm9fi0691plhcwa
foreign key (products_PRODUCT_ID)
references Product
Hibernate:
alter table MEMBER_PRODUCT
add constraint FKp9hlrsu8hrsusdymar0ddcl9o
foreign key (members_MEMBER_ID)
references Member
|
Entity 실행 결과
⭐ 다대다 매핑은 일대다 + 다대일 매핑으로 풀어내야 함
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
public class Member {
@Id @GeneratedValue
@Column(name = "MEMBER_ID")
private Long id;
@Column(name = "USER_NAME")
private String name;
@OneToMany(mappedBy = "member")
private List<MemberProduct> memberProducts = new ArrayList<>();
}
|
Member Entity
- Member : MemberProduct = 1: 다
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
public class Product {
@Id @GeneratedValue
@Column(name = "PRODUCT_ID")
private Long id;
@Column(name = "PRODUCT_NAME")
private String name;
@OneToMany(mappedBy = "product")
private List<MemberProduct> memberProducts = new ArrayList<>();
}
|
Product Entity
- Product : MemberProduct = 1: 다
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import javax.persistence.*;
import java.time.LocalDateTime;
@Entity
public class MemberProduct {
@Id @GeneratedValue
private Long id;
@ManyToOne
@JoinColumn(name = "PRODUCT_ID")
private Product product;
@ManyToOne
@JoinColumn(name = "MEMBER_ID")
private Member member;
private int count;
private int price;
private LocalDateTime orderDate;
}
|
MemberProduct Entity
- Member : MemberProduct = 1: 다
- Product : MemberProduct = 1: 다
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
Hibernate:
create table Member (
MEMBER_ID bigint not null,
USER_NAME varchar(255),
primary key (MEMBER_ID)
)
Hibernate:
create table MemberProduct (
id bigint not null,
count integer not null,
orderDate timestamp,
price integer not null,
MEMBER_ID bigint,
PRODUCT_ID bigint,
primary key (id)
)
Hibernate:
create table Product (
PRODUCT_ID bigint not null,
PRODUCT_NAME varchar(255),
primary key (PRODUCT_ID)
)
Hibernate:
alter table Member
add constraint FK332130jlg9s5hyeuk7gfgi052
foreign key (LOCKER_ID)
references Locker
Hibernate:
alter table MemberProduct
add constraint FKjnj8ungt7v35y6lfxuxcrjbbr
foreign key (MEMBER_ID)
references Member
Hibernate:
alter table MemberProduct
add constraint FKrgt6jorh7iaec1tae84ljye8c
foreign key (PRODUCT_ID)
references Product
|
Entity 실행 결과
'Java > JPA' 카테고리의 다른 글
[JPA_Basic] 상속관계 매핑 (0) | 2023.08.23 |
---|---|
[JPA_Basic] ERD를 기반으로 한 Entity 작성 (0) | 2023.08.17 |
[JPA_Basic] 일대다 매핑 시, ArrayList를 사용하는 이유 (0) | 2023.08.15 |
[JPA_Basic] 양방향 매핑 시, 값이 입력되지 않는 경우 (0) | 2023.08.13 |
[JPA_Basic] 객체 지향 모델링 (0) | 2023.08.08 |