Issue
I tried to get data from table B and saved it into Class Object A
A sale = repository.getSale(m, stmtDate);
repository
public interface repository extends JpaRepository<B, String> {
@Query(value = " SELECT ID AS id FROM B...LEFT JOIN C ... ", nativeQuery = true)
A getSale(@Param("m") String m, @Param("date") String stmtDate);
}
Class A
@Entity
public class A implements Serializable {
private String id;
public String getID() {
return id;
}
public void setID(String id) {
this.id = id;
}
}
.....
Error
org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.Object[]] to type [com.rh.app.batch.domain.A] for value '{0e89c64c-d840-45c5-9c6a-bda52fbaa7d1, 201802, 5.000, 1, 0, 0}'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [com.rh.app.batch.domain.A]
Solution
I am not sure if that is what you are looking for but, when you want to map the results of a complex query into one object, you can use the SqlResultSetMapping annotation:
Assuming you have a class like.
public class PersonDTO {
private String name;
private Date birthDate;
//omit other attributes
public Person(String name, Date birthDate) {
this.name = name;
this.birthDate = birthDate;
}
you decorate it as follows.
@SqlResultSetMapping(name="PersonDTOMapping",
classes="@ConstructorResult(
targetClass= PersonDTO.class,
columns = {@ColumnResult(name="name"),
@ColumnResult(name="birhDate")}))
and your query becomes:
PersonDTO person = (PersonDTO) em.createNativeQuery("select p.name, p.birthDate as date from person where p.id=1", "PersonDTOMapping").getSingleResult();
please let me know, if this is what you re looking for.
Answered By - Eirini Graonidou
Answer Checked By - Pedro (JavaFixing Volunteer)