Spring/Spring JPA ๊ธฐ๋ณธ

Spring/Spring JPA ๊ธฐ๋ณธ

[์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ๊ธฐ๋ณธํŽธ] 7. @MappedSuperclass

@MappedSuperclass ์ƒ์†๋ฐ›๋Š” ๊ฒƒ์•„ ์•„๋‹Œ , ํ…Œ์ด๋ธ”๊ณผ ๋งคํ•‘ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ๊ณตํ†ต ์†์„ฑ(๋งคํ•‘ ์ •๋ณด)๋งŒ ์ œ๊ณตํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. @MappedSuperclass public abstract class BaseEntity { private String createdBy; private LocalDateTime createDate; private String lastModifiedBy; private LocalDateTime lastModifiedDate; } ์ง์ ‘ ์ƒ์„ฑํ•ด์„œ ์‚ฌ์šฉํ•  ์ผ์ด ์—†์œผ๋ฏ€๋กœ ์ถ”์ƒ ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค์ž public class Member extends BaseEntity ๋ถ€๋ชจ ํด๋ž˜์Šค๋ฅผ ์ƒ์† ๋ฐ›๋Š” ์ž์‹ ํด๋ž˜์Šค์— ๋งคํ•‘ ์ •๋ณด๋งŒ ์ œ๊ณตํ•œ๋‹ค. ์กฐํšŒ, ๊ฒ€์ƒ‰์ด ๋ถˆ๊ฐ€ํ•˜๋‹ค ( em.find(BaseEntity..

Spring/Spring JPA ๊ธฐ๋ณธ

[์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ๊ธฐ๋ณธํŽธ] 6. ์ƒ์†๊ด€๊ณ„ ๋งคํ•‘ / @Inheritance

์ƒ์†๊ด€๊ณ„ ๋งคํ•‘ ์ƒ์†๊ด€๊ณ„ ๋งคํ•‘์ด๋ž€ , ๊ฐ์ฒด์˜ ์ƒ์† ๊ตฌ์กฐ์™€ DB์˜ ์Šˆํผํƒ€์ž… ์„œ๋ธŒํƒ€์ž… ๊ด€๊ณ„๋ฅผ ๋งคํ•‘ํ•˜๋Š” ๊ฒƒ์ด๋‹ค . ๊ฐ์ฒด ์ƒ์† ๊ตฌ์กฐ ๊ฐ์ฒด์—๋Š” ์ƒ์†๊ด€๊ณ„๊ฐ€ ์žˆ๋‹ค . ์Šˆํผํƒ€์ž…-์„œ๋ธŒํƒ€์ž… ๋…ผ๋ฆฌ ๋ชจ๋ธ ํ•˜์ง€๋งŒ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ์ƒ์† ๊ด€๊ณ„๊ฐ€ ์—†๋‹ค. ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—๋Š” ์Šˆํผํƒ€์ž…,์„œ๋ธŒํƒ€์ž… ๊ด€๊ณ„๋ผ๋Š” ๋ชจ๋ธ๋ง ๊ธฐ๋ฒ•์ด ๊ฐ์ฒด ์ƒ์†๊ณผ ์œ ์‚ฌํ•˜๋‹ค. ์Šˆํผํƒ€์ž… ์„œ๋ธŒํƒ€์ž… ๋…ผ๋ฆฌ ๋ชจ๋ธ์„ ์‹ค์ œ ๋ฌผ๋ฆฌ ๋ชจ๋ธ๋กœ ๊ตฌํ˜„ํ•˜๋Š” 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. JOINED , SINGLE_TABLE , TABLE_PER_CLASS @Inheritance ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ถ€๋ชจ ํด๋ž˜์Šค์—์„œ ์ „๋žต์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋‹ค. JOINED ๊ฐ๊ฐ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๊ณ  ์กฐํšŒํ•  ๋•Œ๋Š” join ํ•˜์—ฌ ๊ฐ€์ ธ์˜จ๋‹ค DTYPE : ํƒ€์ž…์„ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•œ ์ปฌ๋Ÿผ์ด๋‹ค. @Inheritance(strategy =..

Spring/Spring JPA ๊ธฐ๋ณธ

[์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ๊ธฐ๋ณธํŽธ] 5. ๋‹ค๋Œ€์ผ[N:1] / ์ผ๋Œ€๋‹ค[1:N] / ์ผ๋Œ€์ผ[1:1] / ๋‹ค๋Œ€๋‹ค[N:M]

๋‹ค๋Œ€์ผ [N : 1] @ManyToOne ๋‹จ๋ฐฉํ–ฅ Member(N) : Team(1) ์€ ๋‹ค๋Œ€์ผ ๊ด€๊ณ„ ํ•œ์ชฝ์ด ๊ด€๊ณ„๋ฅผ ๋งบ์€ ์ชฝ์˜ ์—ฌ๋Ÿฌ ๊ฐ์ฒด๋ฅผ ๊ฐ–๋Š” ๊ฒƒ์„ ์˜๋ฏธ ํ•˜๋‚˜์˜ ํŒ€(1)์€ ์—ฌ๋Ÿฌ ๋ฉค๋ฒ„(N)๋ฅผ ๊ฐ–๋Š”๋‹ค Member.class @ManyToOne @JoinColumn(name = "team_id") private Team team; Many -> One Member(N) -> Team(1) name ์†์„ฑ์—๋Š” ๋งคํ•‘ํ•  ์™ธ๋ž˜ ํ‚ค ์ด๋ฆ„ ์ง€์ •ํ•œ๋‹ค. ๋‹ค๋Œ€์ผ [N : 1] OneToMany ์–‘๋ฐฉํ–ฅ Team ๊ฐ์ฒด์—์„œ ๋ฐ˜๋Œ€์ชฝ Member ๋ฅผ ์ฐธ์กฐํ•˜๊ธฐ ์œ„ํ•œ List members ๋ฅผ ์ƒ์„ฑํ•ด์ค€๋‹ค. ์ด๋ ‡๊ฒŒ ์ƒ์„ฑํ•ด์ค˜๋„ ํ…Œ์ด๋ธ”์—๋Š” ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๋Š”๋‹ค. @OneToMany(mappedBy = "team") private List memb..

Spring/Spring JPA ๊ธฐ๋ณธ

[์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ๊ธฐ๋ณธํŽธ] 4. ์—ฐ๊ด€๊ด€๊ณ„ ๋งคํ•‘

์—ฐ๊ด€๊ด€๊ณ„ ๋งคํ•‘ ๊ฐ์ฒด์™€ ํ…Œ์ด๋ธ” ์—ฐ๊ด€๊ด€๊ณ„์˜ ์ฐจ์ด๋ฅผ ์ดํ•ดํ•˜์ž ๊ฐ์ฒด์˜ ์ฐธ์กฐ์™€ ํ…Œ์ด๋ธ”์˜ ์™ธ๋ž˜ ํ‚ค๋ฅผ ๋งคํ•‘ ๊ฐ์ฒด๋ฅผ ํ…Œ์ด๋ธ”์— ๋งž์ถ”์–ด ๋ชจ๋ธ๋ง (์—ฐ๊ด€๊ด€๊ณ„๊ฐ€ ์—†๋Š” ๊ฐ์ฒด) Team @Entity public class Team { @Id @GeneratedValue @Column(name = "TEAM_ID") private Long id; @Column(name = "TEAM_NAME") private String name; } Member @Entity public class Member { @Id @GeneratedValue @Column(name = "MEMBER_ID") private Long id; @Column(name = "USERNAME") private String name; @Column(name = "T..

Spring/Spring JPA ๊ธฐ๋ณธ

[์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ๊ธฐ๋ณธํŽธ] 3. ์—”ํ‹ฐํ‹ฐ ๋งคํ•‘

์—”ํ‹ฐํ‹ฐ ๋งคํ•‘ @Entity @Entity public class Member { @Id @GeneratedValue private Long id; private String username; } @Entity๊ฐ€ ๋ถ™์€ ํด๋ž˜์Šค๋Š” JPA๊ฐ€ ๊ด€๋ฆฌ JPA๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ…Œ์ด๋ธ”๊ณผ ๋งคํ•‘ํ•  ํด๋ž˜์Šค๋Š” @Entity ํ•„์ˆ˜ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž ํ•„์ˆ˜(ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์—†๋Š” public ๋˜๋Š” protected ์ƒ์„ฑ์ž) name ์†์„ฑ @Entity(name = "Member") public class Member { JPA์—์„œ ์‚ฌ์šฉํ•  ์—”ํ‹ฐํ‹ฐ ์ด๋ฆ„์„ ์ง€์ •ํ•œ๋‹ค. ๊ธฐ๋ณธ๊ฐ’: ํด๋ž˜์Šค ์ด๋ฆ„์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉ(์˜ˆ: Member) ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์Šคํ‚ค๋งˆ ์ž๋™ ์ƒ์„ฑ jpa: hibernate: ddl-auto: create DDL์„ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰ ์‹œ์ ์— ์ž๋™ ์ƒ์„ฑ ..

Spring/Spring JPA ๊ธฐ๋ณธ

[์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ๊ธฐ๋ณธํŽธ] 2. PersistenceContext (์˜์†์„ฑ ์ปจํ…์ŠคํŠธ)

PersistenceContext (์˜์†์„ฑ ์ปจํ…์ŠคํŠธ) ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ๋ž€ ์—”ํ‹ฐํ‹ฐ๋ฅผ ์˜๊ตฌ ์ €์žฅํ•˜๋Š” ํ™˜๊ฒฝ์ด๋ผ๋Š” ๋œป์ด๋‹ค. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜๊ณผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์‚ฌ์ด์—์„œ ๊ฐ์ฒด๋ฅผ ๋ณด๊ด€ํ•˜๋Š” ๊ฐ€์ƒ์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐ™์€ ์—ญํ• ์„ ํ•œ๋‹ค. ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ €๋ฅผ ํ†ตํ•ด ์—”ํ‹ฐํ‹ฐ๋ฅผ ์ €์žฅํ•˜๊ฑฐ๋‚˜ ์กฐํšŒํ•˜๋ฉด ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ €๋Š” ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์— ์—”ํ‹ฐํ‹ฐ๋ฅผ ๋ณด๊ด€ํ•˜๊ณ  ๊ด€๋ฆฌํ•œ๋‹ค. public Long save(Member member) { em.persist(member); return member.getId(); } EntityManager.persist(member) : ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ €๋ฅผ ์‚ฌ์šฉํ•ด ํšŒ์› ์—”ํ‹ฐํ‹ฐ๋ฅผ ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์— ์ €์žฅํ•œ๋‹ค๋Š” ์˜๋ฏธ! ์‹ค์ œ๋กœ๋Š” DB์— ์ €์žฅํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ๋ฅผ ํ†ตํ•ด์„œ Entity๋ฅผ ์˜์†ํ™”ํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค. ์ •ํ™•ํžˆ ๋งํ•˜๋ฉด persi..

Spring/Spring JPA ๊ธฐ๋ณธ

[์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ๊ธฐ๋ณธํŽธ] 1. JPA ๊ตฌ๋™ ๋ฐฉ์‹

JPA ๊ตฌ๋™ ๋ฐฉ์‹ EntityManagerFactory ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ €๋Š” ์—ฌ๋Ÿฌ ์Šค๋ ˆ๋“œ๊ฐ€ ๋™์‹œ์— ์ ‘๊ทผํ•˜๋ฉด ๋™์‹œ์„ฑ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ ์Šค๋ ˆ๋“œ ๊ฐ„์— ์ ˆ๋Œ€ ๊ณต์œ ํ•˜๋ฉด ์•ˆ ๋œ๋‹ค. ๋™์‹œ์„ฑ(Concurrency): ์œ ์ €๊ฐ€ ์ฒด๊ฐํ•˜๊ธฐ์—๋Š” ๋™์‹œ์— ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฑฐ ์ฒ˜๋Ÿผ ๋ณด์ด์ง€๋งŒ ์‚ฌ์‹ค์€ ์œ ์ €๊ฐ€ ์ฒด๊ฐํ•  ์ˆ˜ ์—†๋Š” ์งง์€ ์‹œ๊ฐ„๋‹จ์œ„๋กœ ์ž‘์—…๋“ค์„ ๋ฒˆ๊ฐˆ์•„๊ฐ€๋ฉด์„œ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋‚ด๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ •ํ•˜๊ณ  ์žˆ๋Š”๋ฐ ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ์—์„œ ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ๋ฏธ๋ฆฌ ์ˆ˜์ •ํ•ด๋ฒ„๋ฆฌ๋ฉด ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ €๋Š” ํ•˜๋‚˜๋ฅผ ๊ณต์œ ํ•˜๋ฉด ์•ˆ ๋˜๊ณ , ์ƒํ™ฉ์— ๋”ฐ๋ผ ๊ณ„์† ๋งŒ๋“ค์–ด์ค˜์•ผํ•œ๋‹ค. ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ €๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ € ํŒฉํ† ๋ฆฌ์ด๋‹ค. EntityManager Entity๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ํด๋ž˜์Šค์ด๋‹ค. ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ € ๋‚ด๋ถ€์— ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ(Persistence Context)..

Spring/Spring JPA ๊ธฐ๋ณธ

[์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ๊ธฐ๋ณธํŽธ] 0. SQL ์ค‘์‹ฌ์ ์ธ ๊ฐœ๋ฐœ ๋ฌธ์ œ์  / ORM ์ด๋ž€ ?

SQL ์ค‘์‹ฌ์ ์ธ ๊ฐœ๋ฐœ ๋ฌธ์ œ์  ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฐ์ฒด์ง€ํ–ฅ ํŒจ๋Ÿฌ๋‹ค์ž„์—์„œ๋Š” ๊ฐ์ฒด๊ฐ€ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•  ๋•Œ ๊ฐ์ฒด(์ •ํ™•ํžˆ๋Š” ๊ฐ์ฒด์˜ ์ฐธ์กฐ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋ณ€์ˆ˜)๋ฅผ ํ†ตํ•ด ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค. ์ด์— ๋ฐ˜ํ•ด, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํŒจ๋Ÿฌ๋‹ค์ž„์€ ํ…Œ์ด๋ธ”์ด ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์„ ์ฐธ์กฐํ•  ๋•Œ ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์˜ PK๋ฅผ FK๋กœ ๊ฐ€์ง์„ ํ†ตํ•ด ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค. ORM Object-relational mapping(๊ฐ์ฒด ๊ด€๊ณ„ ๋งคํ•‘) ๊ฐ์ฒด๋Š” ๊ฐ์ฒด๋Œ€๋กœ ์„ค๊ณ„ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Œ€๋กœ ์„ค๊ณ„ ORM ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ ์ค‘๊ฐ„์—์„œ ๋งคํ•‘ https://www.inflearn.com/course/ORM-JPA-Basic/dashboard ์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ๊ธฐ๋ณธํŽธ - ์ธํ”„๋Ÿฐ | ๊ฐ•์˜ JPA๋ฅผ ์ฒ˜์Œ ์ ‘ํ•˜๊ฑฐ๋‚˜, ์‹ค๋ฌด์—์„œ JPA๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ ๊ธฐ๋ณธ ์ด๋ก ์ด ๋ถ€์กฑํ•˜์‹  ๋ถ„๋“ค์ด JP..

hello_u
'Spring/Spring JPA ๊ธฐ๋ณธ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก (2 Page)