JPQL Not a Selected Expression 错误

JPQL Not a Selected Expression error

我在 ProductList 实体 class 中有以下 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 class

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 class 添加 prodName 那么它会 给出错误 The identification variable 'appDesc' is not defined in the FROM clause.

我如何做 ORDER BY prodDesc 因为我正在使用 ProductListDTO 构造函数

首先,看起来您的构造函数需要 3 列,但您调用时只有 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;
    }
}

祝你好运!