select id, title, content, user_id, (select count(id) from reply_tb where board_id = bt.id) reply_count from board_tb bt;

image.png

이걸 바로 받아볼꺼임 (이건 통상의 방법으로는 받을 수 없고 한번에 받아야 한다)

countDTO생성

package shop.mtcoding.blog.board;

import lombok.AllArgsConstructor;
import lombok.Data;

@AllArgsConstructor
@Data
public class BoardCountDTO {
    private Integer id;
    private String title;
    private String content;
    private Integer userId;
    private Long replyCount;
}

보더 JPA리포지토리

@Query("select new shop.mtcoding.blog.board.BoardCountDTO(b.id, b.title, b.content, b.user.id, (select count(r.id) from Reply r where r.board.id = b.id)) from Board b")
List<BoardCountDTO> findAllWithReplyCount();

이런식으로 쿼리를 써야한다, new뒤에는 경로가 다 들어감

jpa리포지토리는 써둔 타입이 아니면 <board(이부분), Intger> 다른 타입으로는 못넣어 쓰지만

저 문법을 쓰면 가능해진다.