Spring

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..

Spring/Spring JPA ํ™œ์šฉ

[์‹ค์ „! ์Šคํ”„๋ง ๋ถ€ํŠธ์™€ JPA ํ™œ์šฉ1] - 5. ์›น ๊ณ„์ธต ๊ฐœ๋ฐœ

public class MemberForm { @NotEmpty(message = "ํšŒ์› ์ด๋ฆ„์€ ํ•„์ˆ˜ ์ž…๋‹ˆ๋‹ค") private String name; private String city; private String street; private String zipcode; } ํผ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ™”๋ฉด ๊ณ„์ธต๊ณผ ์„œ๋น„์Šค ๊ณ„์ธต์„ ๋ช…ํ™•ํ•˜๊ฒŒ ๋ถ„๋ฆฌํ•œ๋‹ค. @NotEmpty Null, ๋นˆ ๋ฌธ์ž์—ด ๋ถˆ๊ฐ€ @PostMapping("/members/new") public String create(@Valid MemberForm form , BindingResult result) { if (result.hasErrors()){ return "members/createMemberForm"; } @Valid ํŠน์ง• ์œ ํšจ์„ฑ๊ฒ€์‚ฌ , ์ž˜๋ชป๋œ..

Spring/Spring JPA ํ™œ์šฉ

[์‹ค์ „! ์Šคํ”„๋ง ๋ถ€ํŠธ์™€ JPA ํ™œ์šฉ1] - 4. ์ฃผ๋ฌธ ๋„๋ฉ”์ธ ๊ฐœ๋ฐœ

์ฃผ๋ฌธ ๋„๋ฉ”์ธ ๊ฐœ๋ฐœ //== ์ƒ์„ฑ ๋ฉ”์„œ๋“œ ==// public static Order createOrder(Member member, Delivery delivery, OrderItem... orderItems) ๊ฐ€๋ณ€์ธ์ž ํƒ€์ž…... ๋ณ€์ˆ˜๋ช…๊ณผ ๊ฐ™์€ ํ˜•์‹์œผ๋กœ ์„ ์–ธ for (OrderItem orderItem : orderItems) { order.addOrderItem(orderItem); } ๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐœ์ˆ˜๋ฅผ ๋™์ ์œผ๋กœ ์ง€์ •

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