无法使用@Query 注释将本机 sql 结果映射到 dto

问题描述

我已经按照 JPA 接口投影过程将本机 sql 结果映射到自定义 DTO,但我仍然得到 AbstractJpaQuery$TupleConverter$TupleBackedMap 格式的结果。如何将结果映射到所需的 DTO 类?

这是存储库类

@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 接口

public interface ResultDTO{
        Long getId();
        String getAcademicclass();
        String getSubject();
        String getCriteria();

}

实体是:

@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;
}

结果如下

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"

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)