스프링부트 native Query에러 No converter found capable of converting from type

Nam_JU·2022년 4월 23일
1

ErrorLog

목록 보기
15/26

공부 배경

협업을 하면서 나의 게시글 조회 기능을 제작해야 했다.
나의 게시글 조회를 하기 위해서는 스프링부트에서 nativeQuery를 사용해야했는데, 문제는 에러가 생기면서 쿼리의 값이 불러오지 않는 에러가 생겼다.


데이터베이스 상에서는 쿼리가 잘 작동되어 값을 가져온것을 볼 수 있다.


에러 내용

No converter found capable of converting from type

2022-04-23 23:23:42.073 ERROR 19704 --- [io-8080-exec-10] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [dev.sideproject.momo.model.UserPost]] with root cause

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [dev.sideproject.momo.model.UserPost]
	at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:322) ~[spring-core-5.3.18.jar:5.3.18]
	at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:195) ~[spring-core-5.3.18.jar:5.3.18]
	at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:175) ~[spring-core-5.3.18.jar:5.3.18]

설정했던 코드

Repository

native 쿼리 넣기

public interface UserRepository extends CrudRepository <UserEntity, Long> {


  @Query(
            nativeQuery = true,
            value = "SELECT p.topic, p.content, p.create_at\n" +
                    "FROM user_momo u\n" +
                    "    INNER JOIN post p on u.id = p.user_id\n" +
                    "where u.id = :id"
    ) List<UserPost> findByUserPost(@PathVariable("id") Long id);

}

UserService

public interface UserService {

    UserDto create(UserDto dto);
    Collection<UserDto> readAll();
    UserDto read(Long id);
    boolean update(UserDto dto, Long id);
    boolean delete(Long id);

   Collection<UserPost> findByUserPost(Long id);     // 추가! 
  

}

JPA UserService

@Service
public class JpaUserService implements UserService{
    private final UserRepository userRepository;


    public JpaUserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }


   

   @Override
    public Collection<UserPost> findByUserPost(Long id) {
        List<UserPost> userPostList = this.userRepository.findByUserPost(id);
        userPostList.forEach(
                userPost -> userPostList.add(
                        new UserPost(
                                userPost.getTopic(),
                                userPost.getContent(),
                                userPost.getCreateAt()
                        )
                )
        );


     return userPostList;
    }


}

UserPost

쿼리로 불러올 형식의 DTO 추가

public class UserPost {
    private String topic;
    private String content;
    private Instant createAt;

    public UserPost() {
    }

    public UserPost(String topic, String content, Instant createAt) {
        this.topic = topic;
        this.content = content;
        this.createAt = createAt;
    }

    public String getTopic() {
        return topic;
    }

    public void setTopic(String topic) {
        this.topic = topic;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Instant getCreateAt() {
        return createAt;
    }

    public void setCreateAt(Instant createAt) {
        this.createAt = createAt;
    }
}

UserController

@RestController
@RequestMapping("api/user")
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

  

//내가 쓴글 불러오기
    @GetMapping("{id}/post")
    public ResponseEntity<Collection<UserPost>> userPost(
            @PathVariable Long id){

        Collection<UserPost> result = this.userService.findByUserPost(id);
        return  ResponseEntity.ok(result);
    }

해결방법

UserPost를 삭제 UserPost 대신 인터페이스를 생성하여 값을 불러오도록 하였다.

UserPostInterface

public interface UserPostInterface {

    String getTopic();
    String getContent();
    Instant getCreate_At();

}

바뀐 Repository


    @Query(
            nativeQuery = true,
            value = "SELECT p.topic, p.content, p.create_at\n" +
                    "FROM user_momo u\n" +
                    "    INNER JOIN post p on u.id = p.user_id\n" +
                    "where u.id = :id"
    ) List<UserPostInterface> findByUserPost(@PathVariable("id") Long id);


profile
개발기록

1개의 댓글

comment-user-thumbnail
2022년 5월 31일

덕분에 좋은 내용 잘 보고 갑니다
감사합니다.

답글 달기