Closed kwonslog closed 1 month ago
spring-data-jpa 와 EntityManager 가 CRUD 처리를 위해 사용하는 메소드를 비교해 보았다.
persist
save
find
getReference
findById
getReferenceById
findAll
merge
remove
deleteById
findById는 호출 시 쿼리가 실행되고, getReferenceById는 지연 로딩 방식으로 프록시 객체를 통해 실제 값을 사용 할때 쿼리가 실행된다.
@Transactional
jpa는 트랜잭션이 커밋될때 영속성 컨텍스트가 관리하는 엔티티의 변경 사항을 데이터베이스에 반영하는 쿼리를 실행한다.
@Transactional(readonly = true)
읽기 전용 트랜잭션 설정을 하면 조회 성능 향상을 기대 할 수 있다.
영속성 컨텍스트는 엔티티들을 관리하며 엔티티의 연관관계 설정값은 JPA가 쿼리를 실행 할 때 참고한다. 복잡한 쿼리가 필요한 경우에는 직접 쿼리를 작성하는 것이 더 나은 성능을 발휘 할 수 있다.
영속화 컨텍스트의 엔티티 관리 여부는 entityManager.contains(entity) 를 사용하여 확인 할 수 있다. //아래와 같이 주입 받아서 사용 @PersistenceContext 또는 @Autowired private EntityManager entityManager; @Autowired는 내부적으로 @PersistenceContext를 통해 실제 EntityManager 를 주입받는다.(프록시 객체라서 사용 시점에 주입됨)
영속화 컨텍스트의 엔티티 관리 여부는 entityManager.contains(entity) 를 사용하여 확인 할 수 있다.
//아래와 같이 주입 받아서 사용 @PersistenceContext 또는 @Autowired private EntityManager entityManager;
@Autowired는 내부적으로 @PersistenceContext를 통해 실제 EntityManager 를 주입받는다.(프록시 객체라서 사용 시점에 주입됨)
@Autowired
@PersistenceContext
CRUD
persist
메소드 사용save
메소드 사용find
,getReference
메소드 사용findById
,getReferenceById
,findAll
메소드 사용merge
메소드 사용save
메소드 사용remove
메소드 사용deleteById
메소드 사용findById 와 getReferenceById의 차이점
@Transactional
@Transactional(readonly = true)
영속성 컨텍스트
영속화 관리 여부