์ํ(product)๊ณผ ์ฃผ๋ฌธ(order), ๊ทธ๋ฆฌ๊ณ ์ด ๋ ํ ์ด๋ธ ๊ฐ ์ฐ๊ฒฐ ํ ์ด๋ธ์ธ orderProduct๊ฐ ์๋ค๊ณ ํ์.
`deleteAllInBatch()` ๋ฉ์๋๋ก ๋ฆฌ์์ค๋ฅผ ์ ๋ฆฌํ๋ ๊ฒ๊ณผ `deleteAll()` ๋ฉ์๋๋ก ๋ฆฌ์์ค๋ฅผ ์ ๋ฆฌํ๋ ๊ฒ์ ์ฐจ์ด๋ฅผ ์์๋ณด์.
deleteAllInBatch()
๋จผ์ `deleteAllInBatch()` ๋ฉ์๋๋ฅผ ์ฌ์ฉํด ์ ๋ฆฌํด๋ณผ ๊ฒ์ด๋ค.
@AfterEach
void tearDown() {
orderProductRepository.deleteAllInBatch();
productRepository.deleteAllInBatch();
orderRepository.deleteAllInBatch();
}
์ ํ ์คํธ์ฝ๋ ์คํ ์ ๋ฐ์ํ๋ ์ฟผ๋ฆฌ๋ ๋ค์๊ณผ ๊ฐ๋ค.
Hibernate:
delete
from
order_product
Hibernate:
delete
from
product
Hibernate:
delete
from
orders
๊น๋ํ๊ฒ delete ์ฟผ๋ฆฌ ์ธ ๋ฒ๋ง ๋ฐ์ํ ๋ชจ์ต์ ํ์ธํ ์ ์๋ค.
ํ ์คํธ ์ฝ๋ ์คํ ์์์ ์ํฅ์ ๋ฐ๋๋ฐ, ๋ง์ฝ `orderProductRepository.deleteAllInBatch();`๋ฅผ ๊ฐ์ฅ ๋จผ์ ์คํํ์ง ์์๋ค๋ฉด Product๋ฅผ ์ง์ฐ๋ ๊ณผ์ ์์ ํ ์คํธ์ ์คํจํ์ ๊ฒ์ด๋ค.
deleteAll()
์ด๋ฒ์๋ deleteAll() ๋ฉ์๋๋ฅผ ์ฌ์ฉํด๋ณด์.
@AfterEach
void tearDown() {
orderProductRepository.deleteAll();
productRepository.deleteAll();
orderRepository.deleteAll();
}
ํ ์คํธ์ฝ๋ ์คํ ์ ๋ฐ์ํ๋ ์ฟผ๋ฆฌ๋ ๋ค์๊ณผ ๊ฐ๋ค.
์์ deleteAllInBatch()๋ ๊น๋ํ๊ฒ delete ์ฟผ๋ฆฌ ์ธ ๋ฒ๋ง ๋ฐ์ํ ๋ฐ๋ฉด, deleteAll()์ ๊ต์ฅํ ๋ง์ ์ฟผ๋ฆฌ๊ฐ ๋ฐ์ํ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
Hibernate:
select
orderprodu0_.id as id1_1_,
orderprodu0_.created_date_time as created_2_1_,
orderprodu0_.modified_date_time as modified3_1_,
orderprodu0_.order_id as order_id4_1_,
orderprodu0_.product_id as product_5_1_
from
order_product orderprodu0_
Hibernate:
delete
from
order_product
where
id=?
Hibernate:
delete
from
order_product
where
id=?
Hibernate:
select
product0.id as id1_3_,
product0.created_date_time as created_2_3_,
product0.modified_date_time as modified3_3_,
product0.name as order_id4_3_,
product0.price as product_5_3_,
product0.product_number as product_6_3_,
product0.selling_status as product_7_3_,
product0.type as product_8_3_
from
product product0_
Hibernate:
delete
from
product
where
id=?
Hibernate:
delete
from
product
where
id=?
Hibernate:
delete
from
product
where
id=?
Hibernate:
select
order0.id as id1_2_,
order0.created_date_time as created_2_2_,
order0.modified_date_time as modified3_2_,
order0.order_status as order_id4_2_,
order0.registered_date_time as product_5_2_,
order0.total_price as product_6_2_
from
orders order0_
Hibernate:
select
orderprodu0_.order_id ad order_id4_1_0,
orderprodu0_.id as id1_1_0_,
orderprodu0_.id as id1_1_1_,
orderprodu0_.created_date_time as created_2_1_1_,
orderprodu0_.modified_date_time as modified3_1_1_,
orderprodu0_.order_id as order_id4_1_1_,
orderprodu0_.product_id as product_5_1_1_
from
order_product orderprodu0_
where
orderprodu0_.order_id=?
Hibernate:
delete
from
orders
๋ชป๋ณด๋ select ๋ฌธ์ด ์๊ฒผ๋ค..!
where ์ ์ด ์๋๊ฑธ๋ก ๋ณด์, Order ํ ์ด๋ธ ์ ์ฒด๋ฅผ ์กฐํํ
์ดํ OrderProduct๋ฅผ ์ง์ ๋๋ฐ, delete๋ฌธ์ด ๋ ๋ฒ ๋ฐ์ํ ๊ฒ์ ๋ณด๋ฉด ๊ฑด๊ฑด์ด ์ง์ด ๊ฒ์ด๋ค.
(ํ์ฌ OrderProduct ํ ์ด๋ธ์๋ 1๋ฒ๊ณผ 2๋ฒ ์ํ์ ๋ํ ๋ ๊ฐ์ ๋ ์ฝ๋๊ฐ ์กด์ฌํ๋ค.)
๊ทธ๋ฆฌ๊ณ Product๋ฅผ ์ง์ฐ๊ธฐ ์ ์, ๋ Product ํ ์ด๋ธ ์ ์ฒด๋ฅผ ์กฐํํ๋ค.
์ดํ Product๋ฅผ ๊ฑด๊ฑด์ด 3๊ฐ ์ง์ด๋ค. (ํ์ฌ Product ํ ์ด๋ธ์๋ 3๊ฐ์ ๋ ์ฝ๋๊ฐ ์กด์ฌํ๋ค.)
๋ง์ฐฌ๊ฐ์ง๋ก Order๋ฅผ ์ง์ฐ๊ธฐ ์ ์, Order ํ ์ด๋ธ ์ ์ฒด๋ฅผ ์กฐํํ๋ค.
๊ทธ๋ฆฌ๊ณ Order ํ ์ด๋ธ๊ณผ ์ฐ๊ด๊ด๊ณ๋ฅผ ๋งบ๊ณ ์๋ OrderProduct๋ฅผ order_id๋ฅผ ์ด์ฉํด ํ ๋ฒ ๋ ์กฐํํ๋ค.
(์ด์ ์ OrderProduct๋ฅผ ์ด๋ฏธ ์ง์ ๊ธฐ ๋๋ฌธ์) Order๋ง ๊ฑด๊ฑด์ด ์ง์ด๋ค.
deleteAll()์ ๋ด๋ถ์ ์ผ๋ก for๋ฌธ์ ํตํด ๊ฐ ๊ฐ์ฒด์ ๋ํ ๋ชจ๋ ์ฐ๊ด๊ด๊ณ๋ฅผ ํ๋์ฉ ์ง์ฐ๊ธฐ ๋๋ฌธ์ `orderProductRepository.deleteAll();`์ ์ฃผ์์ฒ๋ฆฌ ํ๋๋ผ๋ ํ ์คํธ์ ์คํจํ์ง ์๋๋ค. (order๋ฅผ ์ง์ฐ๋ฉด์ orderProduct๊น์ง ๊ฐ์ด ์ง์์ง)
์ ๋ฆฌ
deleteAll()์ ๋ค์์ ์ฟผ๋ฆฌ๊ฐ ๋ฐ์ํ๊ธฐ ๋๋ฌธ์ ์ฑ๋ฅ ์ ํ๋ก ์ด์ด์ง ์ ์์ด, ๋ณดํต deleteAllInBatch()๋ฅผ ๋ ์ ํธํ๋ ํธ์ด๋ค.
deleteAllInBatch()๋ ํ ๋ฒ์ ๋ฒํฌ ์ฟผ๋ฆฌ๋ก ์ฒ๋ฆฌ๋์ด ์ฑ๋ฅ ๋ฉด์์ ๋ ํจ์จ์ ์ด๊ณ ๊น๋ํ๊ฒ ๋์ํ๋ค.
๊ทธ๋ ๋ค๊ณ deleteAllInBatch()๊ฐ ๋์๋ค๋๊ฑด ์๋๋ค.
deleteAll()์ ์ฐ๊ด๊ด๊ณ๋ฅผ ์ง์ ์ ๋ฆฌํ์ง ์์๋ ๋๋ ํธ๋ฆฌํจ์ด ์์ผ๋ ์ํฉ์ ๋ฐ๋ผ ์ ์ ํ ์ ํํ๋ฉด ๋ ๋ฏํ๋ค!
'๐ป Knowledge > ํ ์คํธ์ฝ๋' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ๋ ๋์ ํ ์คํธ๋ฅผ ์์ฑํ๊ธฐ ์ํ ๊ตฌ์ฒด์ ์กฐ์ธ + ๋์ ์๊ฐ (0) | 2024.12.31 |
|---|---|
| Test Doubles ์ ๋ฆฌ (0) | 2024.12.27 |
| Presentation Layer ํ ์คํธ (1) | 2024.12.27 |
| @SpringBootTest vs @DataJpaTest (0) | 2024.12.19 |
| Persistence Layer ํ ์คํธ (2) | 2024.12.19 |