[TIL] 20220712 캠프 87일차

C____JIN·2022년 7월 12일
1

TIL 1.0

목록 보기
45/78

2차 프로젝트

Users / Place / Comment 연관관계 생성

  • User.java
	... //생략
    
    @JsonIgnore
    @ManyToOne(fetch = LAZY)
    @JoinColumn(name="place_id", nullable = false)
    private Place place;

    @JsonIgnore
    @ManyToOne(fetch = LAZY)
    @JoinColumn(name="user_id", nullable = false)
    private Users users;
    
    ... //생략
    
    //연관관계 설정
    public void placeComment(Place place){
        this.place = place;
        place.getComments().add(this);
    }

    public void UserComment(Users users){
        this.users = users;
        users.getComments().add(this);
    }
  • Place.java
    	... // 생략
       @OneToMany(mappedBy = "place")
       private List<Comment> comments = new ArrayList<>();
       ... // 생략
  • Users.java
     	... // 생략
       @OneToMany(mappedBy = "users", fetch = LAZY)
       private List<Comment> comments = new ArrayList<>();
       ... // 생략

서비스 메서드 생성

  • CommentService.java

    	// comment 생성
       @Transactional
       public Long save(CommentCreateRequestDto commentCreateRequestDto, String email){
           Comment comment = Comment.builder()
                   .comment(commentCreateRequestDto.getComment())
                   .build();
           Users user = userRepository.findByEmail(email).orElseThrow(
                   () -> new NoSuchElementException("일치하는 메일이 없습니다.")
           );
           Place place = placeRepository.findById(commentCreateRequestDto.getPlaceId()).orElseThrow(
                   () -> new NoSuchElementException("일치하는 맛집이 없습니다.")
           );
    
           comment.placeComment(place);
           comment.UserComment(user);
    
           return commentRepository.save(comment).getId();
       }
       
       // comment 조회
       @Transactional
       public List<CommentListResponseDto> commentList(Long placeId){
           Place place = placeRepository.findById(placeId).orElseThrow(
                   () -> new NoSuchElementException("일치하는 맛집이 없습니다")
           );
    
           List<CommentListResponseDto> dtos = new ArrayList<CommentListResponseDto>();
    
           List<Comment> comments = place.getComments();
           for(Comment comment : comments){
               CommentListResponseDto dto = CommentListResponseDto.builder()
                       .id(comment.getId())
                       .userNickname(comment.getUsers().getNickname())
                       .userEmail(comment.getUsers().getEmail())
                       .comment(comment.getComment())
                       .modifiedAt(comment.getModifiedAt())
               .build();
               dtos.add(dto);
           }
           return dtos;
    
       }

마무리

오늘의 오류

  • Data Conversion Error가 발생 했는데, 그 문제는 h2 database관련 문제였다. mysql로 변경해서 진행하닌까 오류 없이 잘 진행되었다.

    튜터님께서 h2에서 data 변환 문제가 종종 난다고 하셨다.

  • Place와 Comment의 연관관계를 만들어 주었는데 Place에 만들어준 Comment List에 값이 저장되지 않는 문제가 발생하였다.
    다른 문제가 아니라 mappedby="place"mappedby="comment"로 잘못 설정하여 List가 저장되지 않고 있던 것이었다.

소감

진짜 별것도 아닌 place때문에 오늘 8시간을 날렸다.... 후...... 오늘은 기절!

그래도 오늘까지 목표로 하였던, 유저 코멘트 맛집 간의 연관 관계는 마무리가 되었다.

profile
개발 블로그🌐 개발일지💻

1개의 댓글

comment-user-thumbnail
2022년 7월 12일

"일치하는 맛집이 없습니다." - place

답글 달기