

1차 캐시라고 한다.// Entity의 ID(PK) 값으로 Entity를 조회함
Users foundUser = entityManager.find(Users.class, entity.getId());
// 직접 호출
entityManager.flush();
// Transaction이 Commit 시 자동으로 flush 호출
transactional.commit();
// Commit과 마찬가지로 JPQL 쿼리 실행 시 자동으로 flush 호출
entityManager.createQuery("select u from Users u where u.age = 20");

JPA Entity의 상태는 4가지로 분류할 수 있다.
EntityManager로 구현된 코드와 함께 4가지의 상태를 살펴보자.
(우리나라 명칭보단 영어 명칭이 직관적이여서 이해하기 쉬움)
Users entity = new Users();
// New -> Persistent
entityManager.persist(entity);
// Detached -> Persistent
entityManager.merge(entity);
// Entity를 영속성 컨텍스트에서 분리하여 준영속 상태로 만듦
entityManager.detach(entity);
// 영속성 컨텍스트를 비워 저장된 Entity를 준영속 상태로 만듦
entityManager.clear();
// 영속성 컨텍스트를 종료시켜 저장된 Entity를 준영속 상태로 만듦
// Transaction이 끝나고 동작
entityManager.close();
entityManager.remove(entity);
JPA Entity 상태 설명
영속성 컨텍스트의 설명 및 그림
Entity Lifecycle 그림
EntityManager의 메소드 설명