Health-Genie

[주의점] 변경감지

j9972 2023. 11. 28. 11:52
728x90

해당 로직을 진행할때 마다 나는 level 필드의 값을 변경 감지를 통해 DB에 저장 시키려고 했다.

 

@Transactional(readOnly = true)
public List<RoutineResponseDto> getAllGenieRoutine(Level level, Long userId) {
    User user = userServiceImpl.findById(userId);
    user.updateLevel(level); // 이 부분 [ 매번 level update ]

    List<Routine> genie = routineRepository.findByLevel(level);
    return genie.stream()
            .map(RoutineResponseDto::ofGenie)
            .collect(toList());
}

 

문제점

updateLevel()을 통해서 level의 변화를 변경 감지를 통헤 Db 자동 저장을 하려고 했는데, update는 되지만, 저장이 되지 않았었다

 

원인

트랜잭션의 옵션 값이 readOnly이기에 transaction 처리가 되지 않았다.

 

해결

ReadOnly 옵션을 지운다

  @Transactional
    public List<RoutineResponseDto> getAllGenieRoutine(Level level, Long userId) {
        User user = userServiceImpl.findById(userId);
        user.updateLevel(level); // 이 부분 [ 매번 level update ]

        List<Routine> genie = routineRepository.findByLevel(level);
        return genie.stream()
                .map(RoutineResponseDto::ofGenie)
                .collect(toList());
    }