λ°μν
μ°κ΄ κ΄κ³ 맀ν μ’ λ₯
- μΌλμΌ(1:1) → @OneToOne
- μΌλλ€(1:N) → @OneToMany
- λ€λμΌ(N:1) → @ManyToOne
- λ€λλ€(N:N) → @ManyToMany
μ°κ΄ κ΄κ³ 맀ν λ°©ν₯
- DB μ€μ¬ μ€κ³ ν μ΄λΈμμ κ΄κ³λ νμ μλ°©ν₯
- JPA κ°μ²΄ μ§ν₯ μ€μ¬ μ€κ³μμλ λ¨λ°©ν₯, μλ°©ν₯ μ‘΄μ¬
μΌλμΌ λ¨λ°©ν₯ 맀ννκΈ°
Cart → Member
μ₯λ°κ΅¬λ-νμ 맀ν ν μ€νΈ
@Autowired
CartRepository cartRepository;
@Autowired
MemberRepository memberRepository;
@Autowired
MemberController memberController;
@PersistenceContext
EntityManager entityManager;
private MemberFormDto createMember() {
MemberFormDto memberFormDto = new MemberFormDto();
memberFormDto.setMemberId("test");
memberFormDto.setPassword("12345678");
memberFormDto.setName("ν
μ€νΈ");
memberFormDto.setPostCode(2222);
memberFormDto.setAddress("ν
μ€νΈ");
memberFormDto.setAdditionalAddress("ν
μ€νΈ");
return memberFormDto;
}
@Test
void μ₯λ°κ΅¬λ_νμ_μν°ν°_맀ν_μ‘°ν_ν
μ€νΈ() {
MemberFormDto memberFormDto = createMember();
Long id = memberController.join(memberFormDto);
Member member = memberRepository.findByMemberId(memberFormDto.getMemberId());
Cart cart = new Cart();
cart.setMember(member);
cartRepository.save(cart);
entityManager.flush();
entityManager.clear();
Cart savedCart = cartRepository.findById(cart.getId())
.orElseThrow(EntityNotFoundException::new);
assertEquals(savedCart.getMember().getId(), member.getId());
}
λ€λμΌ λ¨λ°©ν₯ 맀ννκΈ°
Cart ← CartItem → Item
λ€λμΌ/μΌλλ€ μλ°©ν₯ 맀ννκΈ°
λ€λλ€ λ§€ν
- μ€λ¬΄μμ μ¬μ©λμ§ μμ
πβοΈ μ°κ²° ν μ΄λΈμλ 컬λΌμ μΆκ°ν μ μμ - 1:N ↔οΈ N:1 ννλ‘ κ΅μ°¨ μν°ν°λ₯Ό λ
μ 체 ERD
728x90
λ°μν
'μ€νλ§ > μΌνλͺ° νλ‘μ νΈ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
μ§μ° λ‘λ© (0) | 2022.11.27 |
---|---|
μμμ± μ μ΄ (0) | 2022.11.26 |
νμ΄μ§ κΆν μ€μ νκΈ° (0) | 2022.11.24 |
μν μ€κ³ λ° λ±λ‘νκΈ° (0) | 2022.11.24 |
νμκ°μ κΈ°λ₯ ꡬν (0) | 2022.11.22 |