[개발] 연관 관계 매핑 : 1대1 단방향 매핑
필기자
2022-11-07 12:16
5,953
0
본문
cart와 member 관계
kr/hull/shop/entity/Cart.java 작성
@Entity
@Table(name = "cart")
@Getter
@Setter
@ToString
public class Cart {
@Id
@Column(name = "cart_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
//@OneToOne(fetch = FetchType.EAGER) //즉시 로딩 전략
@OneToOne
@JoinColumn(name="member_id")
private Member member;
}
결과
cart 기능 테스트
kr/hull/shop/repository/CartRepository.java 생성
public interface CartRepository extends JpaRepository<Cart, Long> {
}
kr/hull/shop/entity/CartTest.java 파일 생성
@SpringBootTest
@Transactional
@TestPropertySource(locations="classpath:application-test.properties")
class CartTest {
@Autowired
CartRepository cartRepository;
@Autowired
MemberRepository memberRepository;
@Autowired
PasswordEncoder passwordEncoder;
@PersistenceContext
EntityManager em;
public Member createMember(){
MemberFormDto memberFormDto = new MemberFormDto();
memberFormDto.setEmail("test@email.com");
memberFormDto.setName("홍길동");
memberFormDto.setAddress("서울시 마포구 합정동");
memberFormDto.setPassword("1234");
return Member.createMember(memberFormDto, passwordEncoder);
}
@Test
@Rollback(value = false)
@DisplayName("장바구니 회원 엔티티 매핑 조회 테스트")
public void findCartAndMemberTest(){
Member member = createMember();
memberRepository.save(member);
Cart cart = new Cart();
cart.setMember(member);
cartRepository.save(cart);
em.flush();
em.clear();
Cart savedCart = cartRepository.findById(cart.getId())
.orElseThrow(EntityNotFoundException::new);
assertEquals(savedCart.getMember().getId(), member.getId());
}
}
결과
댓글목록0