JPA Update ( feature. Dirty Check )

Kyle_Kim·2022년 9월 9일
0

JPA에서 업데이트는 따로 repo.save()메소드를 호출해줄 필요가없다.

@Transactional
    public Long update(Long id, PostsUpdateRequestDto requestDto) {
        Posts posts = postsRepository.findById(id)
                .orElseThrow(() -> new IllegalArgumentException("해당 사용자가 없습니다. id=" + id));

        posts.update(requestDto.getTitle(), requestDto.getContent());

        return id;
 	}

JPA의 엔티티 메니저가 활성화된 상태로 트랜잭션 안에서 데이터베이스에서 데이터를 가져온다면 그 데이터는 영속성 컨텍스트가 유지된 상태입니다. (AKA Dirty Check)

즉 Entity의 객체값만 변경하면 별도로 update쿼리를 날릴 필요가 없다는 이야기이다.

profile
Make Things Right

0개의 댓글