微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决无法使用@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 举报,一经查实,本站将立刻删除。