JPA eclipselink oracle db error:不是选定的表达式

时间:2017-05-16 16:13:52

标签: oracle jpa eclipselink

Order                 OrderItem                                     Product
-----------------------------------------------------------------------------
id                    id,orderId,proudctId,unitPrice,quantity       id, barcode

JPA eclipseLink:按订单ID,产品条形码和数量排序订单商品:

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:不是SELECTed表达式

如果在oracle sqlPlus中执行SQL,则没有错误。如果只是按product.barcode而不是lpad(...)订购,它就可以了。

1 个答案:

答案 0 :(得分: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 > ?

另请注意显式连接语法,它比旧的基于逗号的连接语法和表的重命名别名更现代,更清晰。