//simpleJpaRepository
@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);
}
}
[참고] @Transactional(readOnly=true)
- 데이터를 단순 조회만하고 변경하지 않는 트랜잭션에서 readOnly 옵션을 걸어두면 약간의 성능 향상 효과를 가질 수 있음
- flush()를 생략하기 때문
[주의] save() 메소드의 동작방식
- 새로운 엔티티면 저장: persist
- 새로운 엔티티가 아닐 경우 병합: merge
- db에 select 쿼리를 한 번 날리는 것이 단점
- merge는 영속상태의 엔티티가 모종의 이유로 준영속상태로 바뀌었다가 다시 영속화할 때 사용
- 가능하면 merge보다는 변경감지(dirty check) 기능을 이용해야 함