본문 바로가기

TIL(Today Ingwang Learned)

TIL)2022 - 12 - 10(Spring)


 

Spring

if(commentLikeRepository.findByComment(comment).isEmpty()){
    commentLikeRepository.save(new CommentLike(users,comment));
    return new ResponseMsgDto(HttpStatus.OK.value(),"좋아요 성공");
}else{
    commentLikeRepository.deleteByUsersIdAndCommentId(users.getId(), comment.getId());
    return new ResponseMsgDto(HttpStatus.OK.value(),"좋아요 취소");
}

isEmpty를 통해 repository에 데이터가 있는지 없는지 확인 가능!


void deleteByUsersIdAndCommentId(Long usersId,Long commentId);

쿼리문 작성시 void를 통해 반환값을 받지 않을 수 있다.


데이터베이스에서 save,delete를 할 때는 @Transactional 을 붙이지 않아도 실행이 됐고,

update를 할 때는 @Transactional 으 붙여줬어야됐는데 

delete를 할 때 jpa가 지원하는 delete 대신

deleteByUsersIdAndCommentId

를 사용했더니 값이 변경되지 않았고  @Transactional을 붙이고 나니 값이 변경되는 것을 확인했다

이유는 다음에 찾아보자..

'TIL(Today Ingwang Learned)' 카테고리의 다른 글

TIL) 2022 - 12 - 13(Sping)  (0) 2022.12.14
2022 - 12- 12(Spring , Java)  (0) 2022.12.13
TIL)2022 - 12 - 09(Spring)  (0) 2022.12.10
TIL)2022-12-08(Spring)  (0) 2022.12.09
TIL)2022-12-07(Spring, Error)  (2) 2022.12.08


Calendar
«   2025/03   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
Tags
더보기
Archives
Visits
Today
Yesterday