2017-05-16 52 views
0
Order     OrderItem          Product 
----------------------------------------------------------------------------- 
id     id,orderId,proudctId,unitPrice,quantity  id, barcode 

JPA的EclipseLink:列表順序項,以便通過訂單ID,產品條形碼,及數量:JPA的EclipseLink Oracle數據庫錯誤:不是選擇的表達

select * from OrderItem order by orderId asc, 
lpad(product.barcode, 20, '0') desc, quantity asc 

Query: ReadAllQuery(referenceClass=OrderItem sql="SELECT * FROM (SELECT a.*, 
ROWNUM rnum FROM (SELECT DISTINCT t1.ID AS a1, t1.QUANTITY AS a2, 
t1.UNITPRICE AS a3, t1.orderId AS a4, t1.productId AS a5, lpad(t0.barcode, ?, ?) 
FROM Product t0, OrderItem t1 WHERE (t0.ID = t1.productId) ORDER BY t1.orderId 
ASC, lpad(t0.barcode, ?, ?) DESC, t1.QUANTITY ASC) a WHERE ROWNUM <= ?) WHERE 
rnum > ?") 

ERROR ORA-01791:不是選擇的表達

如果在oracle sqlPlus中執行SQL,沒有錯誤。如果只是通過product.barcode而不是lpad(...)來命令,它就可以工作。

回答

0

假設您想通過相同的表達式lpad(p.barcode, ?, ?)進行訂購,請改用其別名。試試這個:

select * 
from (
    select a.*, 
     ROWNUM rnum 
    from (
     select distinct o.ID as a1, 
      o.QUANTITY as a2, 
      o.UNITPRICE as a3, 
      o.orderId as a4, 
      o.productId as a5, 
      lpad(p.barcode, ?, ?) as a6 
     from Product p 
     join OrderItem o on p.ID = o.productId 
     order by a4, 
      a6 desc, 
      a2 
     ) a 
    where ROWNUM <= ? 
    ) 
where rnum > ? 

還要注意明確加入語法,比基於連接語法老逗號和表重命名別名現代和清晰。