λ°μν
μ¦μ λ‘λ© → μν°ν°λ₯Ό μ‘°νν λ μ°κ΄λ μν°ν°λ₯Ό ν¨κ» μ‘°ν
μ¦μ λ‘λ© ν μ€νΈ
@SpringBootTest
@TestPropertySource(locations = "classpath:application-test.properties")
@Transactional
class OrderTest {
/* μ½λ μλ΅ */
@Autowired
OrderItemRepository orderItemRepository;
@Test
void μ¦μ_λ‘λ©_ν
μ€νΈ() {
Order order = this.createOrder();
Long orderItemId = order.getOrderItems().get(0).getId();
entityManager.flush();
entityManager.clear();
OrderItem orderItem = orderItemRepository.findById(orderItemId)
.orElseThrow(EntityNotFoundException::new);
System.out.println("Order class : " + orderItem.getOrder().getClass());
}
}
orderItem λ°μ΄ν° μ‘°ν μ, order_item ν μ΄λΈκ³Ό item, orders, member ν μ΄λΈμ μ‘°μΈν΄μ νκΊΌλ²μ κ°μ Έμ΄
- μΌλμΌ, λ€λμΌλ‘ 맀νν κ²½μ° κΈ°λ³Έ μ λ΅μΈ μ¦μ λ‘λ©μ ν΅ν΄ μν°ν°λ₯Ό ν¨κ» κ°μ Έμ΄
- μ¬μ©νμ§ μλ λ°μ΄ν°λ νκΊΌλ²μ μ‘°ννλ―λ‘ μ±λ₯ λ¬Έμ λ°μ κ°λ₯
μ¦μ λ‘λ©μ μ¬μ©νλ λμ μ μ§μ° λ‘λ© λ°©μμ μ¬μ©ν΄μΌ ν¨. FetchType.LAZY
λ°©μμΌλ‘ μ€μ
OrderItem ν΄λμ€
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "item_id")
private Item item;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "order_id")
private Order order;
@Test
void μ§μ°_λ‘λ©_ν
μ€νΈ() {
Order order = this.createOrder();
Long orderItemId = order.getOrderItems().get(0).getId();
entityManager.flush();
entityManager.clear();
OrderItem orderItem = orderItemRepository.findById(orderItemId)
.orElseThrow(EntityNotFoundException::new);
System.out.println("Order class : " + orderItem.getOrder().getClass());
System.out.println("=======================================");
orderItem.getOrder().getOrderDate();
System.out.println("=======================================");
}
orderItem ν μ΄λΈλ§ μ‘°ν
orderItem.gerOrder().getClass();
κ²°κ³Ό- μ€μ κ°μ²΄κ° μλ νλ‘μ κ°μ²΄λ₯Ό λ£μ΄λ
- νλ‘μ κ°μ²΄λ μ€μ μ¬μ© μμ μ μ‘°ν μΏΌλ¦¬λ¬Έμ΄ μ€νλ¨
728x90
λ°μν
'μ€νλ§ > μΌνλͺ° νλ‘μ νΈ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
μν μμ νκΈ° (0) | 2022.11.28 |
---|---|
Auditing (0) | 2022.11.27 |
μμμ± μ μ΄ (0) | 2022.11.26 |
μ°κ΄ κ΄κ³ 맀ν (0) | 2022.11.25 |
νμ΄μ§ κΆν μ€μ νκΈ° (0) | 2022.11.24 |