JPQL不是选定的表达式错误 [英] JPQL Not a Selected Expression error

查看:83
本文介绍了JPQL不是选定的表达式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ProductList Entity类中具有以下JPQL,该JPQL按预期执行.

I have the following JPQL in ProductList Entity class which executes as expected.

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类

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类添加prodName,则它将 给出错误The identification variable 'appDesc' is not defined in the FROM clause.

What I would like to achieve is add ORDER BY to the query. When p.prodDesc is added to ORDER BY clause, I am getting error not a SELECTed expression because p.prodDesc is not a selected field. if I add prodName from ProductListDTO class then it would give error The identification variable 'appDesc' is not defined in the FROM clause.

当我使用ProductListDTO构造函数时,如何做ORDER BY prodDesc

How can I do ORDER BY prodDesc as I am using ProductListDTO constructor

推荐答案

首先,看起来您的构造函数需要3列,但是如果只有2列,则在调用.

First, looks like your constructor is expecting 3 columns, but you are calling if with only 2 columns.

第二,似乎您的问题是由于SELECT内的CASE构造了ProductListDTO.

Second, seems like your problem is because of the CASE inside the SELECT to construct the ProductListDTO.

我建议将您的逻辑从查询移到构造函数,以进行如下操作:

I would suggest to move your logic from query to the constructor, to make something like this:

    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;
    }
}

祝你好运!

这篇关于JPQL不是选定的表达式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆