【问题标题】:JPQL Not a Selected Expression errorJPQL 不是选定的表达式错误
【发布时间】:2016-02-24 11:03:21
【问题描述】:

我在 ProductList 实体类中有以下 JPQL,它按预期执行。

select DISTINCT new foo.bar.ProductListDTO(p.prodId, " +
CASE WHEN (p.prodId = 'ZCX') THEN CONCAT(p.prodDesc, ' - ', e.userId)
ELSE p.prodDesc END)  from   
ProductList p  LEFT JOIN p.productCatalogueList c  where c.userId='ZAM'

ProductListDTO 类

public ProductListDTO(String prodId, String prodDesc, String userId) {

    this.prodId = prodId;
    this.prodName = prodDesc;
    this.userId = userId;
}

我想要实现的是在查询中添加ORDER BY。当 p.prodDesc 添加到 ORDER BY 子句时,我收到错误 not a SELECTed expression 因为 p.prodDesc 不是选定字段。如果我从ProductListDTO 类中添加prodName 那么它会 报错The identification variable 'appDesc' is not defined in the FROM clause

当我使用 ProductListDTO 构造函数时,我该怎么做ORDER BY prodDesc

【问题讨论】:

    标签: jpa eclipselink jpa-2.0 jpql


    【解决方案1】:

    首先,看起来您的构造函数需要 3 列,但您调用 if 只有 2 列。

    其次,您的问题似乎是因为 SELECT 中的 CASE 构造了 ProductListDTO。

    我建议将你的逻辑从查询转移到构造函数,做这样的事情:

        select DISTINCT new foo.bar.ProductListDTO(p.prodId, p.prodDesc, e.userId) from   
        ProductList p  LEFT JOIN p.productCatalogueList c  where c.userId='ZAM'
        ORDER BY p.prodDesc
    
    public ProductListDTO(String prodId, String prodDesc, String userId) {    
        this.prodId = prodId;
        if ("ZCX".equals(prodId)) {
           this.prodDesc = prodDesc + " - " + userId;
        } else {
           this.prodDesc = prodDesc;
        }
    }
    

    祝你好运!

    【讨论】:

    • 这是将 case 语句移动到构造函数的一个很好的替代想法。谢谢
    猜你喜欢
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    • 2019-07-04
    • 2020-03-09
    • 1970-01-01
    • 2014-07-26
    • 1970-01-01
    • 2013-06-20
    相关资源
    最近更新 更多