Posts
Q&A
์์ฑ ๋ฉ์๋ setter ์ง๋ฌธ
์ถ๊ฐ์ ์ ํฉํ ๋ฆฌ ๋ฉ์๋๋ผ๋ ํจํด์ด ์๋๊ฒ์ ์๊ฒ๋์ด ์ดํดํ๊ฒ ๋์์ต๋๋ค.JPA๋ฟ๋ง ์๋๋ผ ์ฌ๋ฌ๋ชจ๋ก ๋์์ด ๋๋ ๋ช ๊ฐ์ ์ ๋๋ค. =================================================================== ๋ง์ํ์ ๋๋ก OrderItem์ ์ ์ฉํด๋ณด์์ต๋๋ค. public static OrderItem createOrderItem(Item item, int orderPrice, int count) { OrderItem orderItem = OrderItem.builder() .item(item) .orderPrice(orderPrice) .count(count) .build(); item.removeStock(count); return orderItem;}@Builderprivate OrderItem(Item item, int orderPrice, int count) { this.item = item; this.orderPrice = orderPrice; this.count = count;} ํ๋ฐ... ์ ์ ์ ํํ ๋ฆฌ ๋ฉ์๋ ๋ด ์์ฑ์๋ฅผ ๋ฃ์ด์ ๋ง๋๋๊ป๊น? ํ๋ ์๋ฌธ์ด ๋ญ๋๋ค. ์ฆ, @Builderprivate OrderItem(Item item, int orderPrice, int count) { this.item = item; this.orderPrice = orderPrice; this.count = count; item.removeStock(count);} ์ด๋ ๊ฒ ๋ง๋ค์ด์ Service์์ ๋ฐ๋ก ์ฌ์ฉํ๋ฉด.... ์์ฑ์์ ๋น์ง๋์ค ๋ก์ง์ด ๋ค์ด๊ฐ๋๊ฑด ์ข์ง ์์๊ฒ๊ฐ์ ๋๋์ด๋ค์. ํน์ ์์ฑ์๋ ์์ฑ์๋๋ก ์กด์ฌํ๊ณ ๋ค์ํ ๋น์ง๋์ค ๋ก์ง์ ๋ฐ๋ผ์ ์ ์ ํฉํ ๋ฆฌ ๋ฉ์๋๋ค์ด ์ ์์ฑ์๋ฅผ ์ฌ์ฌ์ฉํ๊ฒ ๊ธ ํ๋๊ฒ๋ชฉ์ ์ผ๊น์? ๊ทํ ์ฃผ๋ง์ ๋น ๋ฅธ๋ต๋ณ ๊ฐ์ฌ๋๋ฆฝ๋๋ค~
- 38
- 8
- 8.1K
Q&A
์์ฑ ๋ฉ์๋ setter ์ง๋ฌธ
์ ๊ฐ ์ดํดํ๋๊ฒ ๋ง๋์ง ํ์ธ์ฐจ ์ถ๊ฐ ๋ฌธ์๋๋ฆฝ๋๋ค. ์์ ์ค์์ public static OrderItem createOrderItem(Item item, int orderPrice, int count) { OrderItem orderItem = new OrderItem(); orderItem.setItem(item); orderItem.setOrderPrice(orderPrice); orderItem.setCount(count); item.removeStock(count); return orderItem;} ์ ์์ ๋ ์ ์ ํฉํ ๋ฆฌ ๋ฉ์๋์ด๊ณ Entity๋ฅผ ์์ฑํ๋ ๋ฐฉ์์ด ๋ค์ํ๋ค๋ฉด ์ฐ๋๋ฐฉ์? ์๋๋ ์์ฑ์๋ฅผ ํตํ ์์ฑ private OrderItem(Item item, int orderPrice, int count) { this.item = item; this.orderPrice = orderPrice; this.count = count; item.removeStock(count);} ์๋๋ ์์ฑ์ Builder ํจํด ์ถ๊ฐ @Builderprivate OrderItem(Item item, int orderPrice, int count) { this.item = item; this.orderPrice = orderPrice; this.count = count; item.removeStock(count);} ๋ผ๊ณ ์ดํด๋๊ฒ ๋ง์๊น์? ๋ํ Entity์ ๋น์ง๋์ค ๋ก์ง์ด ๋ค์ด๊ฐ๋๊ฒ ์๋นํ ์ด์ ์ด ์๊ตฌ๋ ์๊ฐ์ด ๋ค๋ฉด์ Set์์ฒด๊ฐ ์์ผ๋ฉด ์๋๋ค๋ ๊ฐ๋ฐ๊ด๋ ์ด ๊ณต๋ถํ๋ค๊ฐ ์๊ฒจ์ ๊น์ํ๋์ ์์ ๋ฅผ ๋ณผ๋์๋นํ ํผ๋์ค๋ฌ์ ์ต๋๋ค. ๋ต๋ณ์ ์ฃผ์ ๊ฒ์ ๊ธฐ๋ฐ์ผ๋ก ์๊ฐํ ๋ Entity์์๋ @Setter๋ ์ฐ์ง์๋๋ค๋จ, ๋ณ๊ฒฝ๋ฉ์๋๋ฅผ ๋ง๋ค์ด์ ์๋ฏธ๋ฅผ ๋ถ์ฌํ๋ค.์ฆ, ์๋์ ๊ฐ์์์ผ๋ก ํ๋ค. public static OrderItem createOrderItem2(Item item, int orderPrice, int count) { OrderItem orderItem = new OrderItem(); orderItem.mappingItem(item); orderItem.mappingOrderPrice(orderPrice); orderItem.mappingCount(count); item.removeStock(count); return orderItem;} ํ์ง๋ง mappingItem ๊ณผ ๊ฐ์ด ์๋ฏธ๊ฐ ์๋ค๋ฉด... @Setter Lombok์ ์ฐ์ง์์ง๋งEntity ๋ด setter ๋ฉ์๋๋ฅผ ํ์์ ๋ฐ๋ผ ๋ง๋ ๋๊ฒ์ ์ค๋ฌด์์๋ ์ฌ์ฉ๋๋ค? ๋ผ๊ณ ํ๋จํด๋ ์ข์๊น์? ํ์ฌ๋ด Boot์ JPA๋ฅผ ๋์ ํด๋ณด๊ณ ์ ์ํ์ ๋ง๋๋๋ฐ ์กฐ๊ธ์ด๋ผ๋ ๋ ๊ณ ๋ฏผํ๊ณ ์ถ์ด๊ณผ์ ๊ณผ ๋ฒ์ด๋ ์ง๋ฌธ์ ๋๋ฆฌ๋๊ฒ๊ฐ์ ์ก๊ตฌํฉ๋๋ค๋ง์ธํ๋ฐ์ ์ด๋ฐ ์ข์ ๊ฐ์๋ฅผ ์ฌ๋ ค์ฃผ์ ๊ฑด ์ ์๊ฒ ํ์ด์ด๋ค์. ๊ฐ์ฌํฉ๋๋ค.
- 38
- 8
- 8.1K