如何使用 IN 修复 Hibernate Named Native Query 的 Where 子句

Posted

技术标签:

【中文标题】如何使用 IN 修复 Hibernate Named Native Query 的 Where 子句【英文标题】:How to fix Hibernate Named Native Query's Where clause with IN 【发布时间】:2019-08-22 01:18:45 【问题描述】:

此查询将从数据库中返回 17 个带有 IN 子句的数据。

但是,如果我为此分配变量,它会返回 0。

有人可以帮我解决问题吗? 谢谢你。

``` @NamedNativeQuery(name = "CustomerPayeeTransaction.getCustomerPayeeTransactionsForTotalTransactionEmail", query = "" +
            "  select cpt.merchant_id, cpt.merchant_transaction_id, cpt.created_datetime, cpt.amount " +
            "  from customer_payee_transactions cpt"+
            "  inner join appl_inte_payment_gateway aipg on cpt.payment_gateway_id = aipg.integrated_payment_gateway_id"+
            "  inner join appl_cus_paye_trx_statuses acpts on cpt.cus_paye_trx_status_id = acpts.cus_paye_trx_status_id"+
            "  where (cpt.payment_gateway_id = :ipgId)"+
            "  and (acpts.name in ('CG_Pending'))")

    ```String statusList = "('CG_Pending','PG_Pending')";
query.setParameter("statusId",statusList);
 @NamedNativeQuery(name = "CustomerPayeeTransaction.getCustomerPayeeTransactionsForTotalTransactionEmail", query = "" +
                "  select cpt.merchant_id, cpt.merchant_transaction_id, cpt.created_datetime, cpt.amount " +
                "  from customer_payee_transactions cpt"+
                "  inner join appl_inte_payment_gateway aipg on cpt.payment_gateway_id = aipg.integrated_payment_gateway_id"+
                "  inner join appl_cus_paye_trx_statuses acpts on cpt.cus_paye_trx_status_id = acpts.cus_paye_trx_status_id"+
                "  where (cpt.payment_gateway_id = :ipgId)"+
                "  and (acpts.name in :statusList)")

【问题讨论】:

【参考方案1】:

我在休眠而不是字符串中找到了答案

statusList = "('CG_Pending','PG_Pending')";

列出 statusIds = Arrays.asList("PG_Success","CG_Pending");

然后 Hibernate 会自动应用所需的条件。

【讨论】:

以上是关于如何使用 IN 修复 Hibernate Named Native Query 的 Where 子句的主要内容,如果未能解决你的问题,请参考以下文章

spring 管理hibernate事务 配置完了报错 Error creating bean with name 'loginAction' defined in file .

Error creating bean with name ‘flywayInitializer‘ defined in class path resource错误修复

部署到远程 Tomcat 时如何修复“嵌套异常是 org.hibernate.exception.JDBCConnectionException”?

这可能吗:JPA/Hibernate query with list property in result?

Mysql数据库中FIND_IN_SET();在HIbernate中怎么使用。比如,如何传参;

无法修复初学者休眠程序中的错误[重复]