java - 无法迭代查询返回的值

标签 java spring hibernate spring-boot

我正在尝试从 native 查询中检索值,但如果我的返回类型为Object[]。我收到类型转换错误。如果我将其设置为 BigInteger[],我只会得到一个值。

    @Query(value = "select  coalesce(sum(individual),0) as individual, 
    coalesce(sum(groups),0) as groups, coalesce(sum(habitation),0) as habitation, coalesce(sum(works),0) as works,"+
        "  coalesce(sum(others),0) as others,"+
        "  coalesce(sum(online),0) as online, coalesce(sum(standalone),0) as standalone, coalesce(sum(excel),0) as excel, coalesce(sum(manual),0) as manual,"+
        "  coalesce(sum(working),0) as working, coalesce(sum(not_working),0) as not_working, coalesce(sum(under_development),0) as under_development,"+
        "  coalesce(sum(nic),0) as nic, coalesce(sum(cgg),0) as cgg, coalesce(sum(portal_others),0) as portal_others"+
        " from( select dept_id,scheme_id, sub_scheme_id,"+
        "      case when beneficiary_type = 1 then 1 else 0 end as individual,"+
        "      case when beneficiary_type = 2 then 1 else 0 end as groups,"+
        "      case when beneficiary_type = 4 then 1 else 0 end as habitation,"+
        "      case when beneficiary_type = 5 then 1 else 0 end as works,"+
        "      case when beneficiary_type = 6 then 1 else 0 end as others,"+
        "      case when system_type = 1 then 1 else 0 end as online,"+
        "      case when system_type = 2 then 1 else 0 end as standalone,"+
        "      case when system_type = 3 then 1 else 0 end as excel,"+
        "      case when system_type = 4 then 1 else 0 end as manual,"+
        "      case when system_type = 1 and system_status = 1 then 1 else 0 end as working,"+
        "      case when system_type = 1 and system_status = 2 then 1 else 0 end as not_working,"+
        "      case when system_type = 1 and system_status = 3  then 1 else 0 end as under_development,"+
        "      case when system_type = 1 and software_developed_by = '1' then 1 else 0 end as cgg,"+
        "      case when system_type = 1 and software_developed_by = '2' then 1 else 0 end as nic,"+
        "      case when system_type = 1 and (software_developed_by = '0' or  software_developed_by = '99') then 1 else 0 end as portal_others"+
        "    from scheme_details " +
        "    where is_deleted is false"+
        "  )t", nativeQuery = true)
public BigInteger[] queryWithCaseValues();

最佳答案

因此,为了检索字段,我必须进行大量转换

 Object[] schemeDetailValues = (Object[])schemeDetailCount[0];
    BigInteger[] retrieveValues = retrieveschemeDetailValues(schemeDetailValues);
        adminDashboardCommand.setIndividualBeneficiaryCount(retrieveValues[0].intValue());
        adminDashboardCommand.setGroupBeneficiaryCount(retrieveValues[1].intValue());
        adminDashboardCommand.setHabitationBeneficiaryCount(retrieveValues[2].intValue());
        adminDashboardCommand.setWorksBeneficiaryCount(retrieveValues[3].intValue());
        adminDashboardCommand.setOthersBeneficiaryCount(retrieveValues[4].intValue());
        adminDashboardCommand.setTotalBeneficiaryCount(retrieveValues[5].intValue());
        adminDashboardCommand.setOnlineSystemsAvailableCount(retrieveValues[6].intValue());
        adminDashboardCommand.setStandaloneSystemsAvailableCount(retrieveValues[7].intValue());
        adminDashboardCommand.setExcelUploadSystemAvailableCount(retrieveValues[8].intValue());
        adminDashboardCommand.setManualSystemsAvailableCount(retrieveValues[9].intValue());
        adminDashboardCommand.setTotalSystemsAvailableCount(retrieveValues[10].intValue());
        adminDashboardCommand.setWorkingSystemsCount(retrieveValues[11].intValue());
        adminDashboardCommand.setNonWorkingSystemsCount(retrieveValues[12].intValue());
        adminDashboardCommand.setUnderDevelopmentSystemsCount(retrieveValues[13].intValue());
        adminDashboardCommand.setNicWebsiteCount(retrieveValues[14].intValue());
        adminDashboardCommand.setCggWebsiteCount(retrieveValues[15].intValue());
        adminDashboardCommand.setOthersWebsiteCount(retrieveValues[16].intValue());
        adminDashboardCommand.setTotalWebsiteCount(retrieveValues[17].intValue());

所以我对 Spring Data 如何简化开发感到困惑。 JPA 似乎只对简单的映射有用。当涉及到 native 查询时,需要进行大量的数据转换。有人可以告诉我为什么这么多企业在复杂性增加时使用它,它和使用 JDBC 一样好。

谢谢, 马诺杰

关于java - 无法迭代查询返回的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46155862/

相关文章:

java - Hibernate事务重复问题

java - 如何使用 Selenium WD + Java + JUnit 通过 js-doc-upload 按钮上传文件?

java - 完成和垃圾收集

java - 来自依赖的 spring 项目的属性文件

使用 JAXB 基于条件的 Java spring XML 绑定(bind)

hibernate - 在双向多对一关系中从非所有者方更新实体

java - 将 HashMap<String, Integer> 转换为 HashMap<String, Double>

java - MySQL 的 Hibernate 类中出现未知实体错误

spring - 如何将 CodePro Analytix 与 Spring MVC 项目一起使用来生成 jUnit?

java - Hibernate、JDBC 和 Java 在大中型结果集上的性能