JiwonDev

JPA #7 ๊ณ ๊ธ‰ ๋งคํ•‘ (์ƒ์† ๊ด€๊ณ„)

by JiwonDev

๐Ÿ“Œ DB์—์„œ์˜ ์ƒ์† ๊ตฌํ˜„

์šฐ์„ , DB์—๋Š” ๊ฐ์ฒด์ฒ˜๋Ÿผ ์ƒ์†์ด๋ผ๋Š” ๊ฐœ๋…์ด ์—†๋‹ค. ๊ตฌ์กฐ๊ฐ€ ๋น„์Šทํ•˜๊ฒŒ ๋ณด์ผ ๋ฟ ์ƒ์†์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๊ธฐ ์–ด๋ ต๋‹ค.

 

๊ฐ์ฒด๋Š” ์ƒ์†๊ด€๊ณ„๋ฅผ ์‰ฝ๊ฒŒ ๊ตฌํ˜„๊ฐ€๋Šฅํ•˜์ง€๋งŒ, DB์—์„œ๋Š” ์ƒ์†์„ ๋งŒ๋“ค๋ ค๋ฉด 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์กด์žฌํ•œ๋‹ค.

๊ทธ๋ž˜์„œ JPA๋„ @Inheritance ์˜ต์…˜์„ ํ†ตํ•ด ์ „๋žต์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ค์–ด๋†จ๋‹ค. ๋ฌผ๋ก  ๋ฌด์Šจ ๋ฐฉ๋ฒ•์„ ์„ ํƒํ•˜๋˜ ๊ฐ์ฒด ์—”ํ‹ฐํ‹ฐ๋Š” ๋˜‘๊ฐ™์ด ๋™์ž‘ํ•œ๋‹ค. DB๋งŒ ๋‹ค๋ฅผ ๋ฟ.

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Item{
	...
}
@Entity
public class Movie extends Item{
	...
}

@Entity
public class Album extends Item{
	...
}

 

  • JOINED, ์กฐ์ธ์ „๋žต (๊ฐ๊ฐ ํ…Œ์ด๋ธ”๋กœ ๋ณ€ํ™˜, Super-Sub Type)

[๊ฐ€์žฅ ์ •๊ทœํ™”๋œ ๋ฐฉ์‹] ์ €์žฅ๊ณต๊ฐ„์€ ํšจ์œจ์ ์ด๋‹ค. ํ•˜์ง€๋งŒ ์กฐํšŒ์‹œ Join์ด ๋„ˆ๋ฌด ๋งŽ์ด ๋ฐœ์ƒํ•˜๊ณ , ์ฟผ๋ฆฌ๊ฐ€ ๋ณต์žกํ•ด์ง„๋‹ค.

 

 

  • SINGLE_TABLE, ๋‹จ์ผ ํ…Œ์ด๋ธ” ์ „๋žต (ํ†ตํ•ฉ ํ…Œ์ด๋ธ” ์‚ฌ์šฉ, SuperDTO)

[๋ฌด์ง€์„ฑ์œผ๋กœ SUPER ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค์–ด ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•] ์„ฑ๋Šฅ์€ ์ข‹์ง€๋งŒ null์ด ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๊ณ , ํ…Œ์ด๋ธ”์ด ๋„ˆ๋ฌด ํฌ๋‹ค

 

 

  • TABLE_PER_CLASS, ๊ตฌํ˜„ ํด๋ž˜์Šค๋ณ„ ํ…Œ์ด๋ธ” ์ „๋žต (์„œ๋ธŒํƒ€์ž… ํ…Œ์ด๋ธ”๋กœ ๋ณ€ํ™˜) - ์‚ฌ์šฉํ•˜์ง€ ๋ง ๊ฒƒ. ๊ตฌ๋ฆฌ๋‹ค.

not null ์ œ์•ฝ์กฐ๊ฑด์„ ๊ฑธ ์ˆ˜ ์žˆ์ง€๋งŒ, ์“ฐ์ง€๋งˆ๋ผ. ์ค‘๋ณต๋˜์–ด ๊ณต๊ฐ„๋„ ๋งŽ์ด๋จน๊ณ  ์„ฑ๋Šฅ๋„ ๊ตฌ๋ฆฌ๊ณ  ํ†ตํ•ฉ ์ฟผ๋ฆฌ๊ฐ€ ์–ด๋ ต๋‹ค.

 

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์–ต๋‹จ์œ„๋กœ ์Œ“์ด๊ณ , ํŒŒํ‹ฐ์…”๋‹ํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋„ ๋งŽ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ ์ƒ์†๊ด€๊ณ„ ํ…Œ์ด๋ธ”์€ ๋ฌธ์ œ๋ฅผ ์ผ์œผํ‚ค๊ธฐ ์‰ฝ๋‹ค. ๋ฌด์กฐ๊ฑด ๊ฐ์ฒด์ง€ํ–ฅ์ ์œผ๋กœ ์ •๊ทœํ™”ํ•˜๋Š”๊ฒŒ ์ข‹์€๊ฒŒ ์•„๋‹ˆ๋ผ ์ƒํ™ฉ์— ๋”ฐ๋ผ ์ ์ ˆํ•˜๊ฒŒ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.

 

โœ” ๋ฐ์ดํ„ฐ ํƒ€์ž…(DTYPE) ๊ฐ™์ด ์ ๊ธฐ

์ฐธ๊ณ ๋กœ ํ…Œ์ด๋ธ”์— ์ž์‹ ์—”ํ‹ฐํ‹ฐ๋ช…(DTYPE)์„ ํ•จ๊ป˜ ๋„ฃ์–ด์ฃผ๋Š”๊ฒŒ ์ข‹๋‹ค. JPA์—์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค์ •ํ•ด ์ค„ ์ˆ˜ ์žˆ๋‹ค.

๋‹จ์ผ ํ…Œ์ด๋ธ” ์ „๋žต์—์„œ๋Š” ํƒ€์ž…๊ตฌ๋ถ„์ด ์–ด๋ ค์›Œ์„œ ๋ฐ˜๋“œ์‹œ DTYPE์„ ๋„ฃ์–ด์ค˜์•ผํ•œ๋‹ค. ๋ฌผ๋ก  ์นผ๋Ÿผ์ด๋ฆ„์€ ๋‹ค๋ฅธ๊ฑธ ์จ๋„ ์ƒ๊ด€์—†์Œ

๋ฌด์Šจ ํƒ€์ž…์ธ์ง€ ๋‚˜ํƒ€๋‚ด๋Š” DTYPE ๊ฐ™์€ ์นผ๋Ÿผ์„ ๋„ฃ์–ด์ฃผ๋Š”๊ฒŒ ์ข‹๋‹ค.

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name=“DTYPE”) // DB ์นผ๋Ÿผ ์ถ”๊ฐ€
public class Item{
	...
}
@Entity // ์•„๋ฌด๊ฒƒ๋„ ์ ์ง€์•Š์œผ๋ฉด, ์—”ํ‹ฐํ‹ฐ ๋ช…(Movie)๊ฐ€ DTYPE์— ๋“ค์–ด๊ฐ„๋‹ค.
public class Movie extends Item{
	...
}

@Entity
@DiscriminatorValue(“Custom_Album”) // ์ด๋ ‡๊ฒŒ ์›ํ•˜๋Š” ์ด๋ฆ„์„ ๋„ฃ์–ด์ค„์ˆ˜๋„ ์žˆ๋‹ค.
public class Album extends Item{
	...
}

 

 

๐Ÿ“Œ @MappedSuperClass, ์ธํ„ฐํŽ˜์ด์Šค์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๊ธฐ

JPA์—์„œ ์ƒ์†์€ ์•„๋ž˜ 2๊ฐ€์ง€ ๊ฒฝ์šฐ๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค.

  • @Entity๋ฅผ ์ƒ์†๋ฐ›๋Š” ๊ฒฝ์šฐ
  • @MappedSuperClass๋ฅผ ์ƒ์†๋ฐ›๋Š” ๊ฒฝ์šฐ

@MappedSuperClass๋Š” ์—”ํ‹ฐํ‹ฐ, ์ƒ์†์ด๋ž‘์€ ์•„๋ฌด๋Ÿฐ ๊ด€๋ จ์ด ์—†๊ณ  ๊ณตํ†ต ๋งคํ•‘ ์ •๋ณด๋ฅผ ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ๋•Œ ์ด์šฉํ•œ๋‹ค.

๊ฐ์ฒด์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฐ˜๋ณต๋˜๋Š” ํ•„๋“œ๋“ค์„ Interface๋กœ ๋ฝ‘์•„ ์‚ฌ์šฉํ•˜๋Š”๊ฑฐ๋ž‘ ๋น„์Šทํ•˜๋‹ค.

id๋ž‘ name์ด ์•ˆ๋“ค์–ด๊ฐ€๋Š” ํ…Œ์ด๋ธ”์€ ์ž˜ ์—†์œผ๋‹ˆ๊นŒ, ์ธํ„ฐํŽ˜์ด์Šค๋กœ ๋ฝ‘์•„์„œ ์‰ฝ๊ฒŒ ์—”ํ‹ฐํ‹ฐ๋ฅผ ๋งŒ๋“ ๋‹ค.

๋ฌผ๋ก  @MappedSuperClass๋ฅผ ์‚ฌ์šฉํ•ด๋„ DBํ…Œ์ด๋ธ”์€ ๋ณ€ํ•จ์—†๋‹ค. ๊ทธ๋ƒฅ Member, Seller๊ฐ€ ๊ฐ๊ฐ ์žˆ๋Š” ๊ฑฐ๋ž‘ ๋™์ผํ•˜๋‹ค.

๋‹จ์ˆœ ํŽธ์˜๊ธฐ๋Šฅ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ด๋„ ์ƒ๊ด€์—†๋‹ค.

@MappedSuperclass
public class BaseEntity {

    private String createdBy;
    private LocalDateTime createdDate;
}
@Entity
public class Member extends BaseEntity {

//    private String createdBy;
//    private LocalDateTime createdDate;
    private String email;
}

@Entity
public class Seller extends BaseEntity {

//    private String createdBy;
//    private LocalDateTime createdDate;
    private String shopName;
}

 

์ด๋Š” ํŽธ์˜๊ธฐ๋Šฅ์— ๊ฐ€๊นŒ์šด๊ฑฐ์ง€, ์—”ํ‹ฐํ‹ฐ๋ฅผ ๋งŒ๋“œ๋Š”๊ฒŒ ์•„๋‹ˆ๋‹ค.

์ž์‹ ํด๋ž˜์Šค์—๊ฒŒ ๋งคํ•‘ ์ •๋ณด๋งŒ ์ œ๊ณตํ•ด์ค„ ๋ฟ, ์•„๋ž˜์™€ ๊ฐ™์€ ์กฐํšŒ๋Š” ๋‹น์—ฐํžˆ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.

๊ทธ๋ž˜์„œ ๊ฐ€๋Šฅํ•˜๋ฉด public abstract class ์ฒ˜๋Ÿผ ์ถ”์ƒํด๋ž˜์Šค๋กœ ๋งŒ๋“œ๋Š” ๊ฑธ ๊ถŒ์žฅํ•œ๋‹ค. ์–ด์ฐจํ”ผ ์ง์ ‘ ์‚ฌ์šฉํ•  ์ผ์ด ์—†์œผ๋‹ˆ๊นŒ

em.find(BaseEntity); // ๋ถˆ๊ฐ€๋Šฅ

์Šคํ”„๋ง์ด๋‚˜ ์Šคํ”„๋ง JPA๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ด ๊ธฐ๋Šฅ์„ ์ด์šฉํ•ด ์ •๋ง ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๐Ÿ“Œ ์š”์•ฝ

์ƒ์†์ด๋ž€ ๊ฐœ๋…์€ ๊ฐ์ฒด์—๋งŒ ์กด์žฌํ•œ๋‹ค.

DB์—์„œ๋Š” ์™„๋ฒฝํ•œ ์ƒ์†์„ ์ง€์›ํ•˜์ง€ ์•Š๊ณ , 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์ด์šฉํ•ด์„œ ์ƒ์†๊ณผ ๋น„์Šทํ•˜๊ฒŒ ๋™์ž‘ํ•˜๋„๋ก ๊ตฌํ˜„ํ•œ๋‹ค.

 

๊ฐ์ฒด ๋„๋ฉ”์ธ ๋ชจ๋ธ์ด ์•„๋ž˜์™€ ๊ฐ™๋‹ค๋ฉด

DB๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๋„๋ฉ”์ธ ๋ชจ๋ธ๊ณผ๋Š” ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Œ์„ ๊ธฐ์–ตํ•˜์ž. ( ์•„๋ž˜๋Š” ์‹ฑ๊ธ€ ํ…Œ์ด๋ธ” ์ „๋žต ์‚ฌ์šฉ )

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name=“DTYPE”) // DB ์นผ๋Ÿผ ์ถ”๊ฐ€
public class Item{
	...
}
@Entity
public class Movie extends Item{
	...
}

@Entity
public class Album extends Item{
	...
}

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

JiwonDev

JiwonDev

ํ™œ๋™ํ•˜๊ธฐ