Skip to content

[4기 박현지] Mission 2: 영속성 컨텍스트 #263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: hyeon-z
Choose a base branch
from
Open
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
@@ -0,0 +1,26 @@
package org.prgms.springbootjpa.mission2.domain.customer;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Table(name = "customers")
public class Customer {
@Id
Long id;
String firstName;
String lastName;

public void changeFirstName(String firstName) {
this.firstName = firstName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package org.prgms.springbootjpa.mission2.domain.customer;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.prgms.springbootjpa.mission1.customer.repository.CustomerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

@SpringBootTest
class CustomerRepositoryTest {
@Autowired
EntityManagerFactory emf;

@Autowired
CustomerRepository repository;

EntityManager entityManager;

EntityTransaction transaction;


@BeforeEach
void setUp() {
entityManager = emf.createEntityManager();
transaction = entityManager.getTransaction();
repository.deleteAll();

Choose a reason for hiding this comment

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

반복되는 EntityManager 생성을 setup method를 통해서 하는건 어떨까요?

Copy link
Author

@hyeon-z hyeon-z Jul 27, 2023

Choose a reason for hiding this comment

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

}

Customer customer = new Customer(1L, "hyeonji", "park");

@Test
void 저장() {
transaction.begin();

entityManager.persist(customer);

transaction.commit();
}

@Test
void 조회_1차캐시_이용() {
transaction.begin();

entityManager.persist(customer);

transaction.commit();

Customer entity = entityManager.find(Customer.class, customer.getId());
assertThat(entity, samePropertyValuesAs(customer));
}


@Test
void 조회_DB_이용() {
transaction.begin();

entityManager.persist(customer);

transaction.commit();

entityManager.detach(customer);

Customer entity = entityManager.find(Customer.class, customer.getId());
assertThat(entity, samePropertyValuesAs(customer));
}

@Test
void 수정_dirtyCheck_적용() {
transaction.begin();

entityManager.persist(customer);

transaction.commit();

transaction.begin();

customer.changeFirstName("hyeonz");

transaction.commit();

Customer entity = entityManager.find(Customer.class, customer.getId());
assertThat(entity.getFirstName(), is("hyeonz"));
}

@Test
void 수정_dirtyCheck_적용_안됨() {
transaction.begin();

entityManager.persist(customer);

transaction.commit();

entityManager.detach(customer);

transaction.begin();

customer.changeFirstName("hyeonz");

transaction.commit();

Customer afterEntity = entityManager.find(Customer.class, customer.getId());
assertThat(afterEntity.getFirstName(), not("hyeonz"));
}

@Test
void 삭제() {
transaction.begin();

entityManager.persist(customer);

transaction.commit();

transaction.begin();

Choose a reason for hiding this comment

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

삭제에 대한 검증이니 transaction.begin()은 remove() 전에 선언하는것은 어떨까요?
find()는 굳이 transaction 안에 있지 않아도 되니까요..!

Copy link
Author

@hyeon-z hyeon-z Jul 27, 2023

Choose a reason for hiding this comment

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

첫 번째 find문은 persistcommit했을 때 잘 저장됐음을 확인하기 위함이고 현재 begin()remove()위에 있는데 혹시 어떤 부분을 말씀하시는 걸까요..?


Customer beforeCommit = entityManager.find(Customer.class, customer.getId());
assertThat(beforeCommit, samePropertyValuesAs(customer));

entityManager.remove(customer);

transaction.commit();

Customer afterCommit = entityManager.find(Customer.class, customer.getId());
assertThat(afterCommit, nullValue());
}
}