5
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [com.example.dto.ExampleDto]
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:321) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:194) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:174) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.data.repository.query.ResultProcessor$ProjectingConverter.convert(ResultProcessor.java:293) ~[spring-data-commons-2.1.5.RELEASE.jar:2.1.5.RELEASE]

The above error is being thrown when I have a query that returns 2 values in a native JPA query is being used. I'm capturing the query response in the DTO below:

@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ExampleDto {
    @Id
    private String name1;
    private int nameFlag;
}

And in a DAO class, I'm calling the native query as below. The query works in SQL Developer and returns 2 records. But when called as below , it throws the above error.

List<ExampleDto> getExampleDto = myJPARepository.
                .findNameObject(uuid);

There is something wrong in the DTO class, which i need to change. Annotations? I'm not sure what is missing here, and try as I might , putting in @Entity annotation, @Data annotation , I'm not able to resolve this error when the query is called.

UPDATE: The native query associated with this is

@Query(value = "select name1, nameFlag from NameTable",
          nativeQuery = true, name = "findNameObject where namekey = ?")
    List<ExampleDto> findNameObject(
            @Param("nameKey") UUID nameKey);
Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
Roe hit
  • 457
  • 1
  • 7
  • 23

2 Answers2

7

This is a bug: https://jira.spring.io/browse/DATAJPA-1714

You can either use JPQL with a constructor expression, an interface projection or a custom method implementation as a workaround.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • Great. Would you be able to give me an existing sample implementation that you may have referred ? Nevertheless, thanks for the answer. – Roe hit Apr 17 '20 at 20:44
  • Not sure what kind of sample you are looking for. Maybe this does help: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.custom-implementations – Jens Schauder Apr 18 '20 at 04:45
  • 1
    [Interface-based Projections](https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections.interfaces) – Vsevolod Golovanov Dec 29 '20 at 08:53
-1

Make sure, your Repository class is using same class, as you are using for extracting the records from native query. Example shown below for XYZDTO

@Repository
public interface XYZRepository extends JpaRepository <XYZDTO, Long> {
}
  • This is not correct. You will get: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type if you do what is suggested. – Paul A. Trzyna Feb 25 '22 at 02:43