Spring/Spring JPA ํ์ฉ
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 ํ์ฉ
์ฃผ๋ฌธ ๋๋ฉ์ธ ๊ฐ๋ฐ //== ์์ฑ ๋ฉ์๋ ==// public static Order createOrder(Member member, Delivery delivery, OrderItem... orderItems) ๊ฐ๋ณ์ธ์ ํ์
... ๋ณ์๋ช
๊ณผ ๊ฐ์ ํ์์ผ๋ก ์ ์ธ for (OrderItem orderItem : orderItems) { order.addOrderItem(orderItem); } ๋ฉ์๋์ ๋งค๊ฐ๋ณ์ ๊ฐ์๋ฅผ ๋์ ์ผ๋ก ์ง์
Spring/Spring JPA ํ์ฉ
@Service @Transactional(readOnly = true) public class MemberService @Service ์คํ๋ง์์ ์๋น์ค๋ฅผ ์ง์ ํ๋ ์ด๋
ธํ
์ด์
. ์ฌ์ค @Component ์ด๋
ธํ
์ด์
๊ณผ ๊ฑฐ์ ์ฐจ์ด๊ฐ ์์ง๋ง ๋น์ฆ๋์ค ๋ก์ง์ ์ํํ๋ ์๋น์ค ๋ ์ด์ด ํด๋์ค ์์ ๋ํ๋ธ๋ค. @Transactional JPA์ ๋ชจ๋ ๋ฐ์ดํฐ ๋ณ๊ฒฝ์ด๋ ๋ก์ง๋ค์ ํธ๋์ญ์
์์์ ์คํ๋๋๋ก ํ๋ค . DB์ ๊ด๋ จ๋, ํธ๋์ญ์
์ด ํ์ํ ์๋น์ค ํด๋์ค ํน์ ๋ฉ์๋์ @Transactional ์ด๋
ธํ
์ด์
์ ๋ฌ์์ฃผ๋ฉด๋๋ค. @Transactional์ด ๋ถ์ ๋ฉ์๋๋ ๋ฉ์๋๊ฐ ํฌํจํ๊ณ ์๋ ์์
์ค์ ํ๋๋ผ๋ ์คํจํ ๊ฒฝ์ฐ ์ ์ฒด ์์
์ ์ทจ์ํ๋ค. readOnly=true - ๋ฐ์ดํฐ์ ๋ณ๊ฒฝ์ด ์๋ ์ฝ๊ธฐ ์ ์ฉ ๋ฉ์๋์ ์ฌ์ฉ..
Spring/Spring JPA ํ์ฉ
์ ํ๋ฆฌ์ผ์ด์
์ํคํ
์ฒ Repository repository: JPA๋ฅผ ์ง์ ์ฌ์ฉํ๋ ๊ณ์ธต, ์ํฐํฐ ๋งค๋์ ์ฌ์ฉ Entity์ ์ํด ์์ฑ๋ DB์ ์ ๊ทผํ๋ ๋ฉ์๋(ex) findAll()) ๋ค์ ์ฌ์ฉํ๊ธฐ ์ํจ ์ํฐํฐ๋ฅผ ์ ์ธํจ์ผ๋ก์จ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๊ตฌ์กฐ๋ฅผ ๋ง๋ค์๋ค๋ฉด, ์ฌ๊ธฐ์ ์ด๋ค ๊ฐ์ ๋ฃ๊ฑฐ๋, ๋ฃ์ด์ง ๊ฐ์ ์กฐํํ๋ ๋ฑ์ CRUD(Create, Read, Update, Delete)๋ฅผ ํด์ผ ์ธ๋ชจ๊ฐ ์๋๋ฐ, ์ด๊ฒ์ ์ด๋ป๊ฒ ํ ๊ฒ์ธ์ง ์ ์ํด์ฃผ๋ ๊ณ์ธต์ด๋ผ๊ณ ์๊ฐํ๋ฉด ๋๋ค. @Repository public class MemberRepository { @PersistenceContext private EntityManager em; @Repository : ์คํ๋ง ๋น์ผ๋ก ๋ฑ๋ก, JPA ์์ธ๋ฅผ ์คํ๋ง ๊ธฐ๋ฐ ์์ธ๋ก ์์ธ ..
Spring/Spring JPA ํ์ฉ
์ํฐํฐ ํด๋์ค ๊ฐ๋ฐ @Entity @Getter @Setter public class Member { @Id @GeneratedValue @Column(name = "member_id") private Long id; @Column์ ๊ฐ์ฒด ํ๋๋ฅผ ํ
์ด๋ธ์ ์ปฌ๋ผ์ ๋งคํ์์ผ์ฃผ๋ ์ด๋
ธํ
์ด์
์
๋๋ค. name : ํ๋์ ๋งคํํ ํ
์ด๋ธ์ ์ปฌ๋ผ ์ด๋ฆ์ ์ง์ ํ๋ค. ์ํฐํฐ์ ์๋ณ์๋ id ๋ฅผ ์ฌ์ฉํ๊ณ PK ์ปฌ๋ผ๋ช
์ member_id ๋ฅผ ์ฌ์ฉํ๋ค. @Embeddable ๋์, ์ฃผ์ ๋ฒ์ง, ์ฃผ์ ์ฐํธ๋ฒํธ๋ฅผ ์ฃผ์๋ผ๋ ๊ฐ์ฒด๋ก ๋ฌถ์ด ํํํ๋ฉด ๋ณด๋ค ๊ฐ์ฒด์งํฅ์ ์ธ ์ฝ๋๊ฐ ๋ ๊ฒ์ด๋ค. @Embeddable public class Address { private String city; private String street; privat..
Spring/Spring JPA ํ์ฉ
Entity(์ํฐํฐ) JPA์์ ์ํฐํฐ๋ ์ฝ๊ฒ ์๊ฐํ๋ฉด, DB ํ
์ด๋ธ์ ๋์ํ๋ ํ๋์ ํด๋์ค๋ผ๊ณ ์๊ฐํ ์ ์์ต๋๋ค. @Entity๊ฐ ๋ถ์ ํด๋์ค๋ JPA๊ฐ ๊ด๋ฆฌํด์ฃผ๋ฉฐ, JPA๋ฅผ ์ฌ์ฉํด์ DB ํ
์ด๋ธ๊ณผ ๋งคํํ ํด๋์ค๋ @Entity๋ฅผ ๊ผญ ๋ถ์ฌ์ผ๋ง ๋งคํ์ด ๊ฐ๋ฅํฉ๋๋ค. ์ํฐํฐ ํด๋์ค๋ ํ
์ด๋ธ๊ณผ ๋งคํ๋๋ ํด๋์ค์ด๋ค. ์ ๋
ธํ
์ด์
์ ์ฌ์ฉํ์ฌ ์ ์ ํ ์ํฐํฐ ํด๋์ค๋ฅผ ๊ตฌ์ฑํ๊ณ , ์์ฑ๋ ์ํฐํฐ ํด๋์ค๋ ๋ช
์ธ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ
์ด๋ธ๊ณผ ๋งคํ๋๋ค. ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ 2์ฐจ์ ํ
์ด๋ธ ์ด ํ
์ด๋ธ์ ์๋น์ค์์ ํ์ํ ์ ๋ณด๋ฅผ ๋ค ์ ์ฅํ๊ณ ํ์ฉํ๊ฒ ๋๋ค. ์๋ ๊ทธ๋ฆผ๊ณผ ๊ฐ์ด ์ธ๋ก์ ์ด ๋ถ๋ถ์ด Column ์ด๊ณ , ๊ฐ๋ก์ ํ ๋ถ๋ถ์ด ์ํฐํฐ ๊ฐ์ฒด๊ฐ ๋๋ค. ์ด ํ
์ด๋ธ ์ ์ฒด๊ฐ ์ํฐํฐ ์ด๊ณ , ๊ฐ 1๊ฐ์ ํ๋ค์ด ์ํฐํฐ ๊ฐ์ฒด๊ฐ ๋๋ ๊ฒ์ด๋ผ๊ณ ์๊ฐํ๋ฉด ๋๋ค...