inblog logo
|
parangdajavous
    Trouble Shooting

    15. 댓글이 없는 게시물 조회 시 오류

    김미숙's avatar
    김미숙
    Jul 15, 2025
    15. 댓글이 없는 게시물 조회 시 오류
    Contents
    BoardRepositoryh2-console🛠 해결

    BoardRepository

    public Board findByIdJoinUserAndReplies(Integer id) { Query query = em.createQuery("select b from Board b join fetch b.user u left join fetch b.replies r join fetch r.user where b.id = :id order by r.id desc", Board.class); // left join (on절은 생략가능하다) -> 객체지향 쿼리 query.setParameter("id", id); return (Board) query.getSingleResult(); }
    notion image
    notion image
     

    h2-console

    notion image
    → query를 확인했을 때 board에서 조회된 값들이 null로 들어가있음
    → reply_tb와 inner join 하면 댓글이 있는 게시물만 조회되므로 댓글이 없는 1번 게시물을 클릭했을 때 오류가 발생한다
     

    🛠 해결

    BoardRepository 수정

    join fetch → left join fetch
    public Board findByIdJoinUserAndReplies(Integer id) { Query query = em.createQuery("select b from Board b join fetch b.user u left join fetch b.replies r left join fetch r.user where b.id = :id order by r.id desc", Board.class); // left join (on절은 생략가능하다) -> 객체지향 쿼리 query.setParameter("id", id); return (Board) query.getSingleResult(); }
    notion image
    notion image
    Share article
    Contents
    BoardRepositoryh2-console🛠 해결

    parangdajavous

    RSS·Powered by Inblog