개발/JAVA

Spring JPA) ConverterNotFoundException 발생

미니백곰 2022. 1. 20. 15:05

예전부터 JPA 설정만 해놓고 전부 nativeQuery만 해서 실질적인 JPA는 다뤄본적이 없다.

 

작업중 커뮤니티의 게시글을 카테고리별로 조회해오는 작업이 생겨 평소처럼 작업을 진행하던 중

org.springframework.core.convert.ConverterNotFoundException: 
No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type

위 에러가 발생하여 구글을 열심히 뒤져본 결과

interface를 정의하여 Repository에 있는 반환값을 Mapping 시켜주는 방법이었다.

 

사용방법은 

public interface CommunityPostPk {

    int getId();
    int getWriterId();
    String getType();
    String getCategory();
    String getTitle();
    String getCreatedAt();
    String getRegDate();
    int getViews();
    String getLinkPath();
    String getProfileImagePath();
}

반환받을 값들을 interface로 정의를 해주고

@Repository
public interface CommunityRepository extends JpaRepository<CommunityPostEntity, String> {

    @Query(value = "SELECT CP.community_post_id                    AS id, " +
            "              CP.writer_id                            AS writerId, " +
            "              substr(CP.category FROM 1 FOR 1)        AS type,    " +
            "              category                                AS category, " +
            "              title                                   AS title, " +
            "              date_add(CP.reg_date, INTERVAL 9 HOUR)  AS createdAt," +
            "              CP.reg_date                             AS regDate, " +
            "              views                                   AS views, " +
            "              CP.link_path                            AS linkPath, " +
            "              ''                                      AS profileImagePath" +
            "       FROM COMMUNITY_POST CP" +
            "       WHERE concat(open_date, '00') < NOW() " +
            "           AND category =:category AND del_yn = 'N' " +
            "           AND CP.reg_date <= :searchDate " +
            "       ORDER BY community_post_id DESC " +
            "       LIMIT :limit OFFSET :offsets", nativeQuery = true)
    ArrayList<CommunityPostPk> communityCategorySelect(@Param("category") String category, 
    						       @Param("searchDate") String searchDate, 
                                                       @Param("limit") int limit, 
                                                       @Param("offsets") int offsets);
}

 

조회를 위해 작성한 ArrayList의 제네릭으로 정의한 인터페이스를 선언하면

{
    "resultCode": "0000",
    "resultMsg": "정상처리",
    "data": [
        {
            "views": 146,
            "title": "장학금 증정",
            "createdAt": "2021-11-12 00:11:21.0",
            "category": "01",
            "writerId": 1,
            "linkPath": "",
            "regDate": "2021-11-11 15:11:21.0",
            "id": 206,
            "type": "0"
        },
        {
            "views": 59,
            "title": "무료체험!",
            "createdAt": "2021-11-10 19:32:36.0",
            "category": "01",
            "writerId": 1,
            "linkPath": "",
            "regDate": "2021-11-10 10:32:36.0",
            "id": 205,
            "type": "0"
        }
}

정상적으로 조회된걸 알수 있다.

 

아...내 3일...

아직도 갈길이 멀다...

 

 

출처 - https://algorithmstudy-mju.tistory.com/153

https://junhyunny.github.io/spring-boot/jpa/junit/spring-data-jpa-group-by/