命令查询通过使用带有null的IN子句或有条件地使用null来获取结果

时间:2016-09-29 19:16:03

标签: java hibernate jpa java-ee named-query

我需要一个命名查询,只有通过设置Query参数才能满足两个命名查询条件。

命名查询,获取记录" softwareVersion"为null或者SoftwareVersion与列表匹配。

@NamedQuery(name = "getIPDetectionDetailsForPanIndia",
            query =  "select cssc.css.sapId,
                             cssc.css.hostName,
                             cssc.cssGoldenConfiguration.category,
                             cssc.cssGoldenConfiguration.parameter,
                             cssc.cssGoldenConfiguration.vendor,
                             cssc.recommendedValue,
                             cssc.actualValue,
                             cssc.cssGoldenConfiguration.id,
                             cssc.cssGoldenConfiguration.information,
                             cssc.css.softwareVersion,
                             jioc.name,
                             cssc.cssGoldenConfiguration.impact,
                             cssc.cssGoldenConfiguration.command
                      from CssComplianceDetail cssc
                             join cssc.css c
                             left join c.cluster club
                             left join clus.jiocenter jioc 
                      where cssc.cssGoldenConfiguration.impact in (:impactType)
                        and cssc.cssGoldenConfiguration.category in (:category)
                        and TO_CHAR(cssc.creationDate, 'yyyy-MM-dd') = TO_CHAR(:date, 'yyyy-MM-dd')
                        and (cssc.css.softwareVersion in (:softwareVersion)
                             or cssc.css.softwareVersion is null)";

命名查询,用于记录" softwareVersion"仅与列表匹配,无需获取null softwareVersion。

@NamedQuery(name = "getIPDetectionDetailsForPanIndiaAcceptNull",
            query = "select cssc.css.sapId,
                            cssc.css.hostName,
                            cssc.cssGoldenConfiguration.category,
                            cssc.cssGoldenConfiguration.parameter,
                            cssc.cssGoldenConfiguration.vendor,
                            cssc.recommendedValue,
                            cssc.actualValue,
                            cssc.cssGoldenConfiguration.id,
                            cssc.cssGoldenConfiguration.information,
                            cssc.css.softwareVersion,
                            jioc.name,
                            cssc.cssGoldenConfiguration.impact,
                            cssc.cssGoldenConfiguration.command
                     from CssComplianceDetail cssc
                            join cssc.css c
                            left join c.cluster club
                            left join clus.jiocenter join
                     where cssc.cssGoldenConfiguration.impact in (:impactType)
                       and cssc.cssGoldenConfiguration.category in (:category)
                       and TO_CHAR(cssc.creationDate, 'yyyy-MM-dd') = TO_CHAR(:date, 'yyyy-MM-dd')
                       and cssc.css.softwareVersion in (:softwareVersion)";

以下是我的Java代码:

 List softwareVersion = new ArrayList();

    if(softwareVersion.contains("All")) {
        query = getEntityManager().createNamedQuery("getIPDetectionCountForPanIndiaAcceptNull");
    } else {
        query = getEntityManager().createNamedQuery("getIPDetectionCountForPanIndia");
    }

    query.setParameter("softwareVersion", softwareVersion);

0 个答案:

没有答案