JAVA 자료구조 (JPA) Embedded Type

2018.05.18 11:06

졸리운_곰 조회 수:33

 

(JPA) Embedded Type

일반적인 테이블 구조의 문제점

일반적인 DB 테이블 구조에 맞춰 엔티티를 만들다보면 아래와 같이 만들게 된다.

 
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
51
52
53
54
55
56
57
 
@Entity
public class Deal {
@Id
private Long id;
 
@Column
private LocalDate saleStartDate;
 
@Column
private LocalDate saleEndDate;
 
@Column
private int normalPrice;
 
@Column
private int discountPrice;
 
public Long getId() {
return id;
}
 
public void setId(Long id) {
this.id = id;
}
 
public LocalDate getSaleStartDate() {
return saleStartDate;
}
 
public void setSaleStartDate(LocalDate saleStartDate) {
this.saleStartDate = saleStartDate;
}
 
public LocalDate getSaleEndDate() {
return saleEndDate;
}
 
public void setSaleEndDate(LocalDate saleEndDate) {
this.saleEndDate = saleEndDate;
}
 
public int getNormalPrice() {
return normalPrice;
}
 
public void setNormalPrice(int normalPrice) {
this.normalPrice = normalPrice;
}
 
public int getDiscountPrice() {
return discountPrice;
}
 
public void setDiscountPrice(int discountPrice) {
this.discountPrice = discountPrice;
}
}

 

객체 지향 관점에서 봤을 때 판매 시작/종료 날짜와 일반가/할인가는 전혀 관련이 없는 데이터이다.
응집력이 낮은 데이터들끼리 모여있으므로 테이블을 아래와 같이 설계하는 게 더 응집도 높은 엔티티가 될 것 같다.

 
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
 
@Entity
public class Period {
@Id
private Long id;
 
@OneToOne
private Deal deal;
 
@Column
private LocalDate saleStartDate;
 
@Column
private LocalDate saleEndDate;
 
public Long getId() {
return id;
}
 
public void setId(Long id) {
this.id = id;
}
 
public Deal getDeal() {
return deal;
}
 
public void setDeal(Deal deal) {
this.deal = deal;
}
 
public LocalDate getSaleStartDate() {
return saleStartDate;
}
 
public void setSaleStartDate(LocalDate saleStartDate) {
this.saleStartDate = saleStartDate;
}
 
public LocalDate getSaleEndDate() {
return saleEndDate;
}
 
public void setSaleEndDate(LocalDate saleEndDate) {
this.saleEndDate = saleEndDate;
}
}

 

응집도가 높은(서로 관련이 있는) 판매 시작/종료 날짜를 포함하는 Period라는 테이블을 따로 파서 엔티티로 만들고,
외래키를 사용하여 Deal 테이블과 매핑하였다.

 
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
 
@Entity
public class Price {
@Id
private Long id;
 
@OneToOne
private Deal deal;
 
@Column
private int normalPrice;
 
@Column
private int discountPrice;
 
public Long getId() {
return id;
}
 
public void setId(Long id) {
this.id = id;
}
 
public Deal getDeal() {
return deal;
}
 
public void setDeal(Deal deal) {
this.deal = deal;
}
 
public int getNormalPrice() {
return normalPrice;
}
 
public void setNormalPrice(int normalPrice) {
this.normalPrice = normalPrice;
}
 
public int getDiscountPrice() {
return discountPrice;
}
 
public void setDiscountPrice(int discountPrice) {
this.discountPrice = discountPrice;
}
}

 

응집도가 높은 일반가/할인가를 포함하는 Period라는 테이블을 따로 파서 엔티티로 만들고,
외래키를 사용하여 Deal 테이블과 매핑하였다.

좀 더 객체지향 관점에서 바라본 Deal 엔티티는 아래와 같이 변경될 것이다.

 
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
 
@Entity
public class Deal {
@Id
private Long id;
 
@OneToOne(mappedBy = "deal")
private Period period;
 
@OneToOne(mappedBy = "deal")
private Price price;
 
public Long getId() {
return id;
}
 
public void setId(Long id) {
this.id = id;
}
 
public Period getPeriod() {
return period;
}
 
public void setPeriod(Period period) {
this.period = period;
}
 
public Price getPrice() {
return price;
}
 
public void setPrice(Price price) {
this.price = price;
}
}

 

외래키는 deal 테이블의 id로 잡아서 각각 period, price 테이블에 뒀다고 가정한다.
위와 같이 테이블을 일일이 쪼개기란 매우 귀찮은 일이다.
그리고 조인을 하기 때문에 쪼개기 전보다 성능상 좋지 않은 점도 많다.

Embedded Type

이럴 때 쓰는 게 바로 Embedded Type이다.
백문이 불여일견, 코드로 바로 보자.

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
@Embeddable
public class Period {
@Column
private LocalDate saleStartDate;
 
@Column
private LocalDate saleEndDate;
 
public LocalDate getSaleStartDate() {
return saleStartDate;
}
 
public void setSaleStartDate(LocalDate saleStartDate) {
this.saleStartDate = saleStartDate;
}
 
public LocalDate getSaleEndDate() {
return saleEndDate;
}
 
public void setSaleEndDate(LocalDate saleEndDate) {
this.saleEndDate = saleEndDate;
}
}

 

EmbeddedType으로 응집도가 높은 판매 시작/종료 날짜를 정의하였다.
EmbeddedType을 정의할 클래스에 @Embeddable 어노테이션을 달아줘야하고,
엔티티가 아니기 때문에 식별자나 조인을 위한 엔티티가 없어도 된다.

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
@Embeddable
public class Price {
@Column
private int normalPrice;
 
@Column
private int discountPrice;
 
public int getNormalPrice() {
return normalPrice;
}
 
public void setNormalPrice(int normalPrice) {
this.normalPrice = normalPrice;
}
 
public int getDiscountPrice() {
return discountPrice;
}
 
public void setDiscountPrice(int discountPrice) {
this.discountPrice = discountPrice;
}
}

마찬가지로 EmbeddedType으로 응집도가 높은 일반가/할인가를 정의하였다.

 
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
 
@Entity
public class DealEmbedded {
@Id
private Long id;
 
@Embedded
private Period period;
 
@Embedded
private Price price;
 
public Long getId() {
return id;
}
 
public void setId(Long id) {
this.id = id;
}
 
public Period getPeriod() {
return period;
}
 
public void setPeriod(Period period) {
this.period = period;
}
 
public Price getPrice() {
return price;
}
 
public void setPrice(Price price) {
this.price = price;
}
}

필드를 일일이 나열하는 대신 응집도가 높은 EmbeddedType을 사용하면 되고,
가져다 쓸 때는 @Embedded 어노테이션을 달아주면 된다.
EmbeddedType을 null로 세팅하면 관련된 필드들이 모두 null로 세팅돼서 DB에 저장된다.

EmbeddedType을 쓰면 테이블을 따로 만들어주지 않아도 되고, 조인을 사용하지 않으니 성능 상 이슈도 없고,
응집도가 높은 클래스끼리 따로 빼놨으니 좀 더 보기 좋은(?) 형태가 됐다고 말할 수 있다.

엔티티 vs 값 타입

@Entity가 안 붙은 걸 전부 값 타입이라고 말할 수 있다.
둘의 차이점은 영속성 컨텍스트에서 추적이 가능하냐, 가능하지 않느냐이다.
엔티티는 식별자(@Id가 붙은 필드)가 존재하여 엔티티의 변화 추적이 가능하지만,
값 타입의 경우에는 해당 값이 바뀌어버리면 식별자를 가지고 있는 게 아니라 추적이 불가능하다.
따라서 값 타입의 경우에는 엔티티에 의존적이고, 변화를 감지해야하는 경우에는 엔티티로 만들어야할 것이다.

간단하게 값 타입에 대해 정리하자면 다음과 같다.

  1. 기본 값 타입
    엄밀히 말하자면 primitive type(int, long, boolean 등등) 만을 뜻하는 것은 아니다.
    불변하는 타입(Integer, String 등등)까지 포함한 경우를 말한다.
  2. Embedded 타입
    위에서 많이 설명했다.
  3. 값 타입 컬렉션
    1과 2 타입을 컬렉션(List, Set, Map 등등)으로 가지고 있는 경우를 말한다.
    많이 쓸까… 싶기도 하고 나중에 자세히 알아봐야겠다.

[출처] https://blog.perfectacle.com/2018/02/19/jpa-embedded-type/

 

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
224 JPA 다대다 관계 모델 @OneToMany로 구현하여 Column 추가하기(@IdClass 사용) file 졸리운_곰 2018.05.18 94
223 Spring Data JPA 연관관계 매핑하는 방법 졸리운_곰 2018.05.18 21
222 공유된 FK(Foreign Key) JPA 연관 관계 매핑 하기 file 졸리운_곰 2018.05.18 676
221 JPA - 05. 연관관계 매핑 기초 졸리운_곰 2018.05.18 31
220 SpringBoot JPA 예제(1:N, 양방향) 졸리운_곰 2018.05.18 28
219 SpringBoot JPA 예제(@OneToMany, 단방향) 졸리운_곰 2018.05.18 27
218 JPA / Hibernate One to Many Mapping Example with Spring Boot file 졸리운_곰 2018.05.18 88
217 The best way to map a @OneToMany relationship with JPA and Hibernate file 졸리운_곰 2018.05.18 177
» (JPA) Embedded Type file 졸리운_곰 2018.05.18 33
215 스프링 데이터 JPA 레퍼런스 번역 file 졸리운_곰 2018.05.14 341
214 UML: 클래스 다이어그램과 소스코드 매핑 file 졸리운_곰 2018.04.30 170
213 lombok에 대해서 알아보자 file 졸리운_곰 2018.04.24 59
212 lombok을 잘 써보자! (2) 졸리운_곰 2018.04.24 151
211 lombok을 잘 써보자! (1) 졸리운_곰 2018.04.24 77
210 Maven 기초 사용법 졸리운_곰 2018.04.15 103
209 [JAVA] Java 와 Mysql 연동 및 DB 사용 졸리운_곰 2018.02.14 88
208 json을 파싱해보자 졸리운_곰 2018.02.12 58
207 [JAVA] json형식의 문자열을 json객체로 parsing하기 졸리운_곰 2018.02.12 90
206 [Java] Quartz (쿼츠)를 사용하여 자바 스케줄링(scheduling) 하기 졸리운_곰 2018.02.12 245
205 스프링(Spring) 프레임워크 기본 개념 강좌 (7) - Patterns 졸리운_곰 2017.10.02 92
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED