Transaction 커밋과 flush 타이밍 이해하기
문제 상황 게시글을 수정하는 단계에서 updated_at 수정 필드가 업데이트가 되지 않아서 찾아보게 됐습니다. @Transactional public Post modify(String title, String body, String username, Integer postId) { // 포스트 찾기 log.info("포스트 찾기"); PostEntity postEntity = postRepository.findById(postId).orElseThrow(() -> new SnsException(Errorcode.NOT_EXISTS_POST, String.format("게시글 ID: %d", postId))); // 생성자, 수정자 일치 확인 log.info("생성자 수정자 일치"); if (!Objects.equals(postEntity.getMember().getName(), username)){ throw new SnsException(Errorcode.INVALID_PERMISSION, String.format("생성자: %s \n 수정자: %s", postEntity.getMember().getName(), username)); } postEntity.setTitle(title); postEntity.setBody(body); PostEntity save = postRepository.save(postEntity); return Post.fromEntity(save); } save VS saveAndFlush save() 메소드 save() 메소드의 특징은 영속성 컨텍스트를 저장하고 Transaction 커밋이 끝나야지만 DB에 flush()가 됩니다. ...