Native Query로 Join 후 두 테이블 이상 select 할 때, entity를 DTO로 매핑하지 못해 발생하는 문제 해결 방법입니다.
JPA로
SELECT a, b FROM A a, B b WHER a.id = b.id;
같은 쿼리를 보내고, DTO로 받고 싶을 때 문제가 발생한다.
조금 간편한 방법(제가 생각하기에 더 나은 방법)에 대해 2번째 포스트를 올렸습니다.
https://kongjino.tistory.com/27
[SpringBoo] JPA 오류: No converter found capable of converting from type - 2
JPA native query에서 DTO로 매핑할 때 호환이 잘 안 되면 이런 문제가 발생한다. 그래서 구글링한 결과 Interface로 받아야 한다고 해서, JPA -> Interface -> DTO -> Service 로 받아서 사용하였다. 근데 그냥 Inter
kongjino.tistory.com
해결방법
1. Interface로 쿼리 결과 받기
2. Interface로 받은 결과를 DTO로 보내기
의 순서로 진행했다.
1번만 진행해도 되는 것 같긴 한데, 일괄적으로 클래스로 처리하고 싶어서 이렇게 했다.
정답인지는 모르겠음.
1. Interface로 결과 받기
JPA
@Query(value = "SELECT a.aId AS aId, b.bId AS bId FROM A a, B b")
Page<responseInterface> findAll();
- JPA는 자동으로 snake case로 변환을 해준다. 이를 다시 camel case로 변환을 해주어야 잘 매핑이 된다.
ResponseInterface
public interface ResponseInterface {
Long getAId();
Long getBId();
}
responseDto
public class ResponseDto {
private final Long aId;
private final Long bId;
public UserResponse(ResponseInterface entity) {
this.aId = entity.getAId();
this.bId = entity.getBId();
}
}
이렇게 생성해 JPA->interface->DTO 순으로 매핑해주면 된다
참고)
스프링부트 native Query에러 No converter found capable of converting from type
나의 게시글 조회를 하기 위해 스프링부트에서 nativeQuery를 사용하게 되었다. 문제는 에러가 생기면서 쿼리의 값이 불러오지 않는 에러가 생김데이터베이스 상에서는 쿼리가 잘 작동되어 값을
velog.io
Spring JPA :: No converter found capable of converting from type
I am using spring with JPA and trying to execute the query using @query with SQL query and trying to map the result to an object. I have different entity classes and mapping to another DTO as I do ...
stackoverflow.com