Home > Back-end >  Error mapping JPA custom-query to my DTO model
Error mapping JPA custom-query to my DTO model

Time:10-21

I've created a custom query that returns data from different tables in a new table. However JPA is not able to map the result to my DTO object.

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

This is my model class

@Data
public class myModel {

    private String name;
    
    private Integer id;
}

and my Repository with the custom query:

@Query(value = "SELECT name, id_tr AS id FROM (SELECT A.nombre name, D.id_transaccion from tableA A LEFT JOIN ....  RESULTED WHERE RESULTED.id_tr IN (?1)", nativeQuery = true)
    List<myModel> findNameOperation(List<Integer> idList);

What is the problem? I have named the attributes of my DTO in the same way as the fields that would come out in my table. I read this article Error is "No converter found capable of converting from type [java.lang.String] to type [com.marter.travel.model.Picture]" but I'm not using mongo, my database is mysql.

CodePudding user response:

Add constructor to myModel :

package foo;
public class myModel {
    private String name;
    private Integer id;

    public myModel(String name, Integer id) {
      this.name = name;
      this.id = id;
    }
    //getter/setter
}

Then try this :

@Query(value = "SELECT new foo.myModel(name, id_tr AS id) FROM ....")

I'm not sure about the "AS id". You may have to remove it.

  • Related