I have followed JPA interface projection process to map native SQL result into a custom DTO but still, I am getting result in AbstractJpaQuery$TupleConverter$TupleBackedMap
format. How can I map the result into desired DTO class?
This is the repository class
@Repository
public interface SubjectMarkingCriteriaRepository extends JpaRepository<SubjectMarkingCriteria,Long> {
@Query(
value = "select smc.id as id ,c.class as academicClass,s.subject as subject,mc.criteria as criteria " +
"from subject_marking_criteria smc " +
"join subject s on smc.subject_id = s.id " +
"join marking_criteria mc on smc.marking_criteria_id = mc.id " +
"Join class c on s.class_id = c.id ",
nativeQuery = true)
List<ResultDTO> findAllByParam();
}
DTO interface
public interface ResultDTO{
Long getId();
String getAcademicClass();
String getSubject();
String getCriteria();
}
Entities are :
@Data
@Entity
@Table(name = "subject_marking_criteria")
public class SubjectMarkingCriteria {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private Long id;
@Column(name="subject_id")
private Long subjectId;
@Column(name="marking_criteria_id")
private Long markingCriteriaId;
}
@Data
@Entity
@Table(name = "subject")
public class Subject {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private Long id;
@Column(name="subject")
private String subject;
@Column(name="bn_subject")
private String bnSubject;
}
@Data
@Entity
@Table(name = "marking_criteria")
public class MarkingCriteria {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private Long id;
@Column(name="criteria")
private String criteria;
@Column(name="bn_criteria")
private String bnCriteria;
}
Result found as below
results = {ArrayList@12758} size = 2
0 = {$Proxy210@12766} "org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap@1c56a65c"
1 = {$Proxy210@12767} "org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap@31c7eaa"
question from:
https://stackoverflow.com/questions/66059832/can-not-map-native-sql-result-into-dto-using-query-annotation 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…