【问题标题】:JPA With CriteriaQuery and ORDER BY multiplication of values具有 CriteriaQuery 和 ORDER BY 乘法的 JPA
【发布时间】:2019-03-15 12:51:50
【问题描述】:

我放弃了为上一个问题寻找解决方案:Spring data - Order by multiplication of columns,因为我找不到任何方法来实现它。

问题是,我将EntityManagerCriteriaQuery 一起使用,并且我试图找到一种方法来放置一个ORDER BY 子句,但将两列相乘。 前任。您存储在 DB 中的数量和单位价格,并且您想按总价(数量 * unitPrice)订购它。

但是当我尝试cq.orderBy(cb.asc(r.get("amount * unitPrice"))); 时,即使这两个属性都在我的 DTO 中,我总是得到java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [amount * unitPrice]。显然,我知道为什么会出现该异常,但我找不到任何解决方法来运行此类查询。

我不能使用命名查询,因为我的查询需要一个我动态应用的过滤器 -> 我不能放置“where id in ()”,因为用户不能选择任何 ID,这意味着他想显示所有他们和我的命名查询显然不会返回任何内容。当然,我不能在后端对其进行排序,因为记录太多了。

我一直在寻找实现它的东西这么久,但没有找到任何解决方案,我不敢相信这是不可能的。

【问题讨论】:

    标签: jpa spring-data-jpa entitymanager hibernate-criteria javax.persistence


    【解决方案1】:

    找到实现这一点的语法:

    cq.orderBy(cb.asc(cb.prod(r.get("amount"), r.get("unitPrice"))));
    

    其中 cq 是 CriteriaQuery,cb 是 CriteriaBuilder,r 是 Root

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-14
      • 2011-04-11
      • 1970-01-01
      • 2017-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多