【问题标题】:Convert Oracle NVl QUery to Criteria Query将 Oracle NVl 查询转换为条件查询
【发布时间】:2018-12-10 11:56:30
【问题描述】:

有没有办法将此 Oracle Query 转换为 Criteria Query。

SELECT * FROM T_MERCHANT_ORDER_DETAILS where MERCHANT_ID = in_merchantId and ORDER_ID= nvl(in_orderId,ORER_ID) and 
TRANSACTION_ID=nvl(in_txnId,TRANSACTION_ID); 

in_merchantId 和 in_orderId 是传递给 oracle 过程的变量。 我可以在 Hibernate 中执行此操作吗?

注意::: 如果 orderId 和 txnId 为 null,则返回基于 MercerId 的结果集, 或者如果只有 txnId 为 null,则为 MercerId 和 orderId, 如果没有一个为空,则在所有三个上。

提前致谢。

【问题讨论】:

  • 为什么不呢?如果 orderId/tx 为 null 则省略谓词,否则添加它们...这样 nvl 功能将移至 java 代码,但我在您的示例中没有看到问题

标签: java spring oracle hibernate


【解决方案1】:

我这样做是为了让它工作。

if(orderId==null && transactionId ==null) {
        criteria.select(root).where(builder.equal(root.get("merchantId"),merchantId));
    }
    else if(transactionId==null && orderId!=null) {
        criteria.select(root).where(builder.equal(root.get("merchantId"),merchantId),builder.equal(root.get("orderId"),orderId));
    }
    else {
        criteria.select(root).where(builder.equal(root.get("merchantId"),merchantId),builder.equal(root.get("transactionId"),transactionId));
    }

但我认为可以用更好的代码来完成。

【讨论】:

    猜你喜欢
    • 2021-05-19
    • 2019-07-29
    • 2018-11-01
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 2011-06-12
    相关资源
    最近更新 更多