Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Embeddable
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Address {

@Column(name = "street_address", nullable = false)
Expand All @@ -15,5 +18,9 @@ public class Address {
@Column(name = "zip_code", nullable = false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

각 필드마다 한 줄 띄어보는 건 어떨까요? 가독성 향상 될 것 같습니다~

private Integer zipCode;


public Address(String streetAddress, String detailedAddress, Integer zipCode) {
this.streetAddress = streetAddress;
this.detailedAddress = detailedAddress;
this.zipCode = zipCode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.programmers.springbootjpa;

import static org.assertj.core.api.Assertions.assertThat;

import com.programmers.springbootjpa.domain.Address;
import com.programmers.springbootjpa.domain.Customer;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.EntityTransaction;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class PersistenceContextTest {

@Autowired
EntityManagerFactory entityManagerFactory;

@Test
@DisplayName("고객을 영속 상태로 만들 수 있다.")
void persistCustomer() {
// given
EntityManager entityManager = entityManagerFactory.createEntityManager();
EntityTransaction entityTransaction = entityManager.getTransaction();

entityTransaction.begin();
Customer customer = Customer.builder()
.name("이현호")
.nickName("황창현")
.age(27)
.address(new Address("서울특별시 영등포구 도신로", "XX빌딩", 10000))
.build();

// when
entityManager.persist(customer);
entityTransaction.commit();

Customer persistedCustomer = entityManager.find(Customer.class, 1L);

// then
assertThat(persistedCustomer).usingRecursiveComparison().isEqualTo(customer);
}

@Test
@DisplayName("고객을 준영속 상태로 만들 수 있다.")
void semiPersistCustomer() {
// given
EntityManager entityManager = entityManagerFactory.createEntityManager();
EntityTransaction entityTransaction = entityManager.getTransaction();

entityTransaction.begin();
Customer customer = Customer.builder()
.name("이현호")
.nickName("황창현")
.age(27)
.address(new Address("서울특별시 영등포구 도신로", "XX빌딩", 10000))
.build();

// when
entityManager.persist(customer);
entityTransaction.commit();

entityManager.detach(customer);
boolean isSemiPersisted = entityManager.contains(customer);

// then
assertThat(isSemiPersisted).isFalse();
}

@Test
@DisplayName("고객을 비영속 상태로 만들 수 있다.")
void nonPersistCustomer() {
// given
EntityManager entityManager = entityManagerFactory.createEntityManager();
EntityTransaction entityTransaction = entityManager.getTransaction();

entityTransaction.begin();
Customer customer = Customer.builder()
.name("이현호")
.nickName("황창현")
.age(27)
.address(new Address("서울특별시 영등포구 도신로", "XX빌딩", 10000))
.build();

// when
entityTransaction.commit();

boolean isNonPersisted = entityManager.contains(customer);

// then
assertThat(isNonPersisted).isFalse();
}

@Test
@DisplayName("고객을 영속성 컨텍스트에서 삭제할 수 있다.")
void removeCustomerFromPersistenceContext() {
// given
EntityManager entityManager = entityManagerFactory.createEntityManager();
EntityTransaction entityTransaction = entityManager.getTransaction();

entityTransaction.begin();
Customer customer = Customer.builder()
.name("이현호")
.nickName("황창현")
.age(27)
.address(new Address("서울특별시 영등포구 도신로", "XX빌딩", 10000))
.build();

// when
entityManager.persist(customer);
entityTransaction.commit();
entityManager.remove(customer);

boolean isRemovedFromPersistenceContext = entityManager.contains(customer);
Customer actualCustomer = entityManager.find(Customer.class, 1L);

// then
assertThat(isRemovedFromPersistenceContext).isFalse();
assertThat(actualCustomer).isNull();
}

}

This file was deleted.