카테고리 없음
JPA save, flush, commit..@Transaction
하얀잔디
2022. 8. 25. 15:56
JPA의 save(Entity) 는
영속성 컨텍스트 em.flush() 나 em.commit() 을 요청하는줄 알았으나, 아니었다!!
@Transactional
@Override
public <S extends T> S save(S entity) {
Assert.notNull(entity, "Entity must not be null.");
if (entityInformation.isNew(entity)) {
em.persist(entity);
return entity;
} else {
return em.merge(entity);
}
}
save는 단순하게 persist 하거나, merge 하는거였다..!
준영속상태란?
변경 감지 기능을 사용하면 원하는 속성만 선택해서 변경할 수 있지만,
병합을 사용하면 모든 속성이 변경된다. 병합시 값이 없으면 null 로 업데이트 할 위험도 있다.
아무튼 save할때 merge나 persist를 호출함으로써 영속성 컨텍스트에 보관하고,
@Transaction이 끝날때 ( OSIV 설정을 따로 안했을때는 controller, filter가 끝난 후 ) flush()를 하는 구조이다.