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

如何使用JPA本机查询和TypedParameterValue执行计数

如何解决如何使用JPA本机查询和TypedParameterValue执行计数

我有一个Entity类,我想计算Postgresql数据库中满足某些条件的元素数。

该实体包含许多列,但我只需要一个响应即可返回所选列的计数,包括未决,已批准,未提交,已提交,已批准,未批准,正在查询...

为了避免与Postgresql驱动程序类型不匹配,我使用TypedParameterValue方法。但是,我得到一个空值作为响应,而不是查询获取的实际值。

例如,我得到了:

{
    "status": "OK","success": true,"errorCode": 0,"data": [
        null
    ],"message": "See Data Object for Details"
 

代替:


"status": "OK","data": {
        "approved": 1000,"totalApplications": 2000,"submitted": 560,"notSubmitted": 880,"pending": 30,"queried": 60,"notApproved": 70
    },"message": "See Data Object for Details"
 

这是我从邮递员那里发送的请求

{
"startDate": "2014-06-11","endDate": "2020-10-14","processtypeFk":9542,"companyTypeId": 6995

    }

下面是我的代码

    @PostMapping("/date-sum")
    public ResponseEntity<Object> sumAnnualReturnRecordsWithDate(@RequestBody SearcherDto searcherDto) {

        Long companyType = searcherDto.getCompanyTypeId();
        Long processtype = searcherDto.getProcesstypeFk();
        Long processtype1 = searcherDto.getProcesstypeFk1();
        Long processtype2 = searcherDto.getProcesstypeFk2();
        Long processtype3 = searcherDto.getProcesstypeFk3();
        LocalDateTime start = LocalDateTime.of(LocalDate.from(searcherDto.getStartDate()),LocalTime.of(0,0));
        LocalDateTime end = LocalDateTime.of(LocalDate.from(searcherDto.getEndDate()),LocalTime.of(23,59,59));


             Query q = em.createNativeQuery("SELECT null AS id,sum(case when (IS_QUERIED = false AND SUBMITTED = true) then b.AMOUNT else null end) AS pending," +
                     " sum(case when (SUBMITTED = false OR SUBMITTED IS NULL) then b.AMOUNT else null end) AS notSubmitted," +
                     " sum(case when ( (SUBMITTED = true)) then 1 else null end) AS submitted," +
                     " sum(b.AMOUNT) AS totalApplications," +
                     " sum(case when (IS_QUERIED = true ) then b.AMOUNT else null end) AS queried," +
                     " sum(case when (APPROVED = true) then b.AMOUNT else null end) AS approved," +
                     " sum(case when (APPROVED = false) then b.AMOUNT else null end) AS notApproved " +
                     " FROM ANNUAL_RETURNS a " +
                     " LEFT JOIN PAYMENT_HISTORY b ON b.PROCESS_TYPE_FK = a.PROCESS_TYPE_FK" +
                     " LEFT JOIN COMPANY c ON c.id= a.COMPANY_FK " +
                     " WHERE (a.FINANCIAL_YEAR_END >=:startDate AND a.FINANCIAL_YEAR_END <=:endDate) AND " +
                     " b.PAYMENT_STATUS='APPROVED' AND (c.COMPANY_TYPE_FK=:companyTypeId OR :companyTypeId=0) " +
                     " AND (a.PROCESS_TYPE_FK =:processtypeFk OR " +
                     " (:processtypeFk1=9542 AND :processtypeFk2=9594 AND :processtypeFk3=9598)) ",AnnualReturn.class);

        q.setParameter("processtypeFk",new TypedParameterValue(LongType.INSTANCE,processtype));
        q.setParameter("processtypeFk1",processtype1));
        q.setParameter("processtypeFk2",processtype2));
        q.setParameter("processtypeFk3",processtype3));
        q.setParameter("companyTypeId",companyType));
        // these are never null,so we don't need to type-set them.
        q.setParameter("startDate",start);
        q.setParameter("endDate",end);
        List <AnnualReturn> countList=q.getResultList();


        return ResponseEntity.ok(new JsonResponse("See Data Object for Details",countList));
    }

这是主要的Entity类:

@Entity
@Table(name="ANNUAL_RETURNS")
public class AnnualReturn implements Serializable {

    private static final long serialVersionUID = 1L;


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private Long id;

    @Column(name = "IS_QUERIED")
    private Boolean queried;

    @Column(name = "FINANCIAL_YEAR_END")
    private String financialYearEnd;
    @Column(name = "FINANCIAL_YEAR_START")
    private String financialYearStart;

    @Column(name = "FINANCIAL_YEAR")
    private String financialYear;

    private Boolean approved;

    @Column(name = "APPRoval_DATE")
    private Date approvalDate;

    @ManyToOne
    @JoinColumn(name = "COMPANY_FK")
    private Company company;

    @ManyToOne
    @JoinColumn(name = "PROCESS_TYPE_FK",referencedColumnName = "id")
    private Process_Type processtype;

}

请帮助我提供想法和可能的解决方案。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。