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

7-3) ๊ณ ๊ธ‰ ๋งคํ•‘ - ์‹ค์ „ ์˜ˆ์ œ4

iseunghan 2020. 11. 6. 10:42
๋ฐ˜์‘ํ˜•

 

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

 

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

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

www.inflearn.com

 

 

 

โœ…์š”๊ตฌ ์‚ฌํ•ญ ์ถ”๊ฐ€

  • ์ƒํ’ˆ์˜ ์ข…๋ฅ˜๋Š” ์Œ๋ฐ˜, ๋„์„œ, ์˜ํ™”๊ฐ€ ์žˆ๊ณ  ์ดํ›„ ๋” ํ™•์žฅ๋  ์ˆ˜ ์žˆ๋‹ค.
  • ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋Š” ๋“ฑ๋ก์ผ๊ณผ ์ˆ˜์ •์ผ์ด ํ•„์ˆ˜๋‹ค.

 

 

@MappedSuperclass
public class BaseEntity {
	private String createdBy;
    private LocalDateTime createdDate;
    private String lastModifiedBy;
    private LocalDateTime lastModifiedDate;
    
    //getter, setter ์ƒ๋žต
}

์ฒซ๋ฒˆ์งธ ์š”๊ตฌ์‚ฌํ•ญ์„ ๋งŒ์กฑ์‹œํ‚ค๊ธฐ ์œ„ํ•ด, BaseEntity๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•ด์„œ ๊ณตํ†ต ๋งคํ•‘ ์ •๋ณด๋ฅผ ์ƒ์† ์‹œํ‚ค๊ธฐ ์œ„ํ•ด

@MappedSuperclass ์• ๋…ธํ…Œ์ด์…˜์„ ์ถ”๊ฐ€ํ•ด ์ค€๋‹ค.

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn
public abstract class Item extends BaseEntity {
	@Id @GeneratedValue
    private Long id;
    
    //... ์ƒ๋žต
}

@DiscriminatorColumn์˜ ๊ธฐ๋ณธ๊ฐ’์€ DTYPE์ด๋ฏ€๋กœ ์ƒ๋žต์‹œ์ผœ๋„ ๋œ๋‹ค.

@Entity
public class Album extends Item {
	
    private String artist;
    private String etc;
    
    //... ์ƒ๋žต
}

 

@Entity
public class Book extends Item {
	
    private String author;
    private String ibsn;
    
    //... ์ƒ๋žต
}

 

@Entity
public class Movie extends Item {
	
    private String director;
    private String actor;
    
    //... ์ƒ๋žต
}
๋ฐ˜์‘ํ˜•