JPA를 사용한 벌크성 수정 쿼리

public int bulkAgePlus(int age) {

	return em.createQuery("update Member m set m.age = m.age + 1 where m.age >= :age")
										 .setParameter("age", age)
										 .executeUpdate();

}

Spring Data JPA를 사용한 벌크성 수정 쿼리

@Modifying(clearAutomatically = true)
@Query("update Member m set m.age = m.age + 1 where m.age >= :age")
int bulkAgePlus(@Param("age") int age);

참고 : 벌크 연산은 영속성 컨텍스트를 무시하고 실행하기 때문에, 영속성 컨텍스트에 있는 엔티티 상태와 DB에 있는 엔티티 상태가 달라질 수 있다.

  1. 영속성 컨텍스트에 엔티티가 없는 상태에서 벌크 연산을 먼저 수행한다.
  2. 부득이하게 영속성 컨텍스트에 엔티티가 있으면 연산 직후 영속성 컨텍스트를 초기화 한다.