org.springframework.data.jpa.repository.support.SimpleJpaRepository
@Repository
@Transactional(readOnly = true)
public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T, ID> {
@Transactional
@Override
public <S extends T> S save(S entity) {
// ...
}
}
@Repository
적용
@Transactional
트랜잭션 적용
@Transactional(readOnly = true)
readOnly = true
옵션을 사용하면 플러시를 생략해서 약간의 성능 향상을 얻을 수 있음엔티티가 영속성 컨텍스트에 관리되면 1차 캐시부터 변경 감지까지 얻을 수 있다. 하지만 스냅샷을 보관하는 등 더 많은 메모리를 사용하는 단점이 있다. 이를 해결하는 방법이 있다.
select o.id, o.name from Order
@Transactional(readOnly = true
)
save()
메서드*
if (entityInformation.isNew(entity)) {
em.persist(entity);
return entity;
} else {
return em.merge(entity);
}
entityInformation.isNew(entity)
Persistable
인터페이스를 구현해서 판단 로직 변경 가능 package org.springframework.data.domain;
public interface Persistable<ID> {
ID getId();
boolean isNew();
}
참고: JPA 식별자 생성 전략이
@GenerateValue
면save()
호출 시점에 식별자가 없으므로 새로운 엔티티로 인식해서 정상 동작한다. 그런데 JPA 식별자 생성 전략이@Id
만 사용해서 직접 할당이면 이미 식별자 값이 있는 상태로save()
를 호출한다. 따라서 이 경우merge()
가 호출된다.merge()
는 우선 DB를 호출해서 값을 확인하고, DB에 값이 없으면 새로운 엔티티로 인지하므로 매우 비효율 적이다. 따라서Persistable
를 사용해서 새로운 엔티티 확인 여부를 직접 구현하게는 효과적이다. 참고로 등록시간(@CreatedDate
)을 조합해서 사용하면 이 필드로 새로운 엔티티 여부를 편리하게 확인할 수 있다. (@CreatedDate
에 값이 없으면 새로운 엔티티로 판단)
@EntityListeners(AuditingEntityListener.class)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Item implements Persistable<String> {
@Id
private String id;
@CreatedDate
private LocalDateTime createdDate;
public Item(String id) {
this.id = id;
}
@Override
public String getId() {
return id;
}
@Override
public boolean isNew() {
return createdDate == null;
}
}