Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
492 views
in Technique[技术] by (71.8m points)

spring - Can not map native sql result into dto using @Query annotation

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...