๐ŸŒป JAVA/์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ

7-1) ๊ณ ๊ธ‰๋งคํ•‘ : ์ƒ์†๊ด€๊ณ„ ๋งคํ•‘

iseunghan 2020. 11. 5. 16:58
๋ฐ˜์‘ํ˜•

 

์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ๊ธฐ๋ณธํŽธ์„ ๊ณต๋ถ€ํ•˜๋ฉฐ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

 

์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ๊ธฐ๋ณธํŽธ - ์ธํ”„๋Ÿฐ

JPA๋ฅผ ์ฒ˜์Œ ์ ‘ํ•˜๊ฑฐ๋‚˜, ์‹ค๋ฌด์—์„œ JPA๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ ๊ธฐ๋ณธ ์ด๋ก ์ด ๋ถ€์กฑํ•˜์‹  ๋ถ„๋“ค์ด JPA์˜ ๊ธฐ๋ณธ ์ด๋ก ์„ ํƒ„ํƒ„ํ•˜๊ฒŒ ํ•™์Šตํ•ด์„œ ์ดˆ๋ณด์ž๋„ ์‹ค๋ฌด์—์„œ ์ž์‹ ์žˆ๊ฒŒ JPA๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ดˆ๊ธ‰ ์›น ๊ฐœ๋ฐœ ํ”„๋กœ๊ทธ๏ฟฝ

www.inflearn.com

 

โœ…์ƒ์†๊ด€๊ณ„ ๋งคํ•‘

์ฃผ์š” ์• ๋…ธํ…Œ์ด์…˜

  • @Inheritance(strategy = InheritanceType. OOO)
    • JOINED : ์กฐ์ธ ์ „๋žต
      • @DiscripminatorColumn ์ ์šฉํ•ด์ค˜์„œ ๊ตฌ๋ณ„ํ•˜๋Š”๊ฒƒ์ด ์ข‹๋‹ค!
    • SINGLE_TABLE : ๋‹จ์ผ ํ…Œ์ด๋ธ” ์ „๋žต
      • @DiscripminatorColumn ๊ธฐ๋ณธ์œผ๋กœ ์ ์šฉ๋จ!
    • (์ถ”์ฒœํ•˜์ง€ ์•Š๋Š”๋‹ค)TABLE_PER_CLASS : ๊ตฌํ˜„ ํด๋ž˜์Šค๋งˆ๋‹ค ํ…Œ์ด๋ธ” ์ „๋žต
      • ํ…Œ์ด๋ธ” ๋งˆ๋‹ค ๊ตฌ๋ณ„๋˜๊ธฐ ๋•Œ๋ฌธ์— DTYPE์˜ ํ•„์š”๊ฐ€ ์—†์–ด์ง„๋‹ค.
  • @DiscriminatorColumn(name="DTYPE") 
    • ๊ธฐ๋ณธ๊ฐ’์€ DTYPE์ด๋‹ค. DTYPE์—๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์—”ํ‹ฐํ‹ฐ ๋ช…์ด ์ฐํžˆ๊ฒŒ ๋œ๋‹ค.
  • @DiscripminatorValue("OO");
    • DTYPE์— ์—”ํ‹ฐํ‹ฐ ๋ช…์ด ์ฐํžˆ๋Š”๊ฒŒ ์‹ซ๋‹ค๋ฉด ์ด ์• ๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

 

 

๐Ÿš€ ์กฐ์ธ ์ „๋žต

  • ์žฅ์ 
    • ํ…Œ์ด๋ธ” ์ •๊ทœํ™”
    • ์™ธ๋ž˜ ํ‚ค ์ฐธ์กฐ ๋ฌด๊ฒฐ์„ฑ ์ œ์•ฝ์กฐ๊ฑด ํ™œ์šฉ๊ฐ€๋Šฅ
    • ์ €์žฅ๊ณต๊ฐ„ ํšจ์œจํ™”
  • ๋‹จ์ 
    • ์กฐํšŒ์‹œ ์กฐ์ธ์„ ๋งŽ์ด ์‚ฌ์šฉ, ์„ฑ๋Šฅ ์ €ํ•˜
    • ์กฐํšŒ ์ฟผ๋ฆฌ๊ฐ€ ๋ณต์žกํ•จ
    • ๋ฐ์ดํ„ฐ ์ €์žฅ์‹œ INSERT SQL 2๋ฒˆ ํ˜ธ์ถœ
@Entity
@Inheritance(strategy = InheritanceType.JOINED) // ์กฐ์ธ ์ „๋žต์ด๊ธฐ ๋•Œ๋ฌธ์— joined๋กœ ์„ค์ •ํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค.
@DiscriminatorColumn // ๊ธฐ๋ณธ๊ฐ’์ด name = "DTYPE"
public abstract class Item {
    @Id @GeneratedValue
    private Long id;

    private String name;
    private int price;

   // getter, setter ์ƒ๋žต
}

 

----- ๊ณตํ†ต ๋ถ€๋ถ„ ์—”ํ‹ฐํ‹ฐ -----

@Entity
@DiscriminatorValue("A") // DTYPE์„ ์›ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ์ปค์Šคํ…€ ํ• ์ˆ˜ ์žˆ๋‹ค.
public class Album extends Item{
    private String artist;
}

 

@Entity
@DiscriminatorValue("B")
public class Book extends Item{
    private String author;
    private String isbn;
}

 

@Entity
@DiscriminatorValue("M")
public class Movie extends Item{

    private String director;
    private String actor;

   //getter,setter ์ƒ๋žต
  
}

 

 

๐Ÿš€ ๋‹จ์ผ ํ…Œ์ด๋ธ” ์ „๋žต

  • ์žฅ์ 
    • ์กฐ์ธ์ด ํ•„์š” ์—†์œผ๋ฏ€๋กœ ์ผ๋ฐ˜์ ์œผ๋กœ ์กฐํšŒ ์„ฑ๋Šฅ์ด ๋น ๋ฆ„
    • ์กฐํšŒ ์ฟผ๋ฆฌ๊ฐ€ ๋‹จ์ˆœํ•จ
  • ๋‹จ์ 
    • ์ž์‹ ์—”ํ‹ฐํ‹ฐ๊ฐ€ ๋งคํ•‘ํ•œ ์ปฌ๋Ÿผ์€ ๋ชจ๋‘ null ํ—ˆ์šฉ
    • ๋‹จ์ผ ํ…Œ์ด๋ธ”์— ๋ชจ๋“  ๊ฒƒ์„ ์ €์žฅํ•˜๋ฏ€๋กœ ํ…Œ์ด๋ธ”์ด ์ปค์งˆ ์ˆ˜ ์žˆ๋‹ค. ์ƒํ™ฉ์— ๋”ฐ๋ผ์„œ ์กฐํšŒ ์„ฑ๋Šฅ์ด ์˜คํžˆ๋ ค ๋Š๋ ค์งˆ ์ˆ˜ ์žˆ๋‹ค.
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE) 
@DiscriminatorColumn // ๊ธฐ๋ณธ์ ์šฉ!
public abstract class Item {
    @Id @GeneratedValue
    private Long id;

    private String name;
    private int price;

   // getter, setter ์ƒ๋žต
}

 

 

๐Ÿš€ ๊ตฌํ˜„ ํด๋ž˜์Šค๋งˆ๋‹ค ํ…Œ์ด๋ธ” ์ „๋žต

์ด ์ „๋žต์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ค๊ณ„์ž์™€ ORM ์ „๋ฌธ๊ฐ€ ๋‘˜ ๋‹ค ์ถ”์ฒœ X

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) 
public abstract class Item {
    @Id @GeneratedValue
    private Long id;

    private String name;
    private int price;

   // getter, setter ์ƒ๋žต
}

 

๋ฐ˜์‘ํ˜•