2015-10-20 70 views
0

我是新來的JPQL和掙扎了一下翻譯我的SQL來JPQL。我有一個簡單的查詢,這在MySQL的工作原理:MySQL來JPQL內加入

SELECT askorder.price, bidorder.price from OrderBook JOIN askorder 
ON OrderBook.ASKORDERID_ORDERID = askorder.ORDERID 
JOIN bidorder ON OrderBook.BIDORDERID_ORDERID = bidorder.ORDERID 
WHERE askorder.ORDERID = 2; 

我已經嘗試此轉化爲JPQL但我敢肯定這是錯的。具體來說,我認爲這可能是ON關鍵字,我不知道如何翻譯,爲JPQL:

@NamedQuery(
    name="findOrderBooksPrices", 
    query="SELECT ao.price, bo.price from OrderBook bo, OrderBook ao" + 
    "INNER JOIN ao ON bo.ASKORDERID_ORDERID = ao.ORDERID " + 
    "INNER JOIN bo ON OrderBook.BIDORDERID_ORDERID = bo.ORDERID " + 
    "WHERE ao.ORDERID = :askOrderId" 
) 

我得到的錯誤是:

Internal Exception: Exception [EclipseLink-7158] (Eclipse Persistence 
Services - 2.5.0.v20130507-3faac2b): 
org.eclipse.persistence.exceptions.ValidationException 
Exception Description: Error encountered when building the @NamedQuery 
[findOrderBooksPrices] from entity class [class 
org.eclipse.persistence.internal.jpa.metadata.queries. 
NamedQueryMetadata].Internal Exception: java.lang.ClassCastException: 
org.eclipse.persistence.jpa.jpql.parser.NullExpression cannot be cast to 
org.eclipse.persistence.jpa.jpql.parser.IdentificationVariable. Please 
see server.log for more details. 

我想它返回提交訂單ID後,每個訂單表的價格。

感謝您尋找。 乾杯, Zerb

+0

爲什麼宥調用表'ao'這裏'INNER JOIN AO ON'但在覈心的MySQL調用表'JOIN askorder ON'等什麼是真實的表名? – Alex

+0

真正的表名是askorder。我剛剛聽到我的語法困惑。 – zobbo

回答

0

只是一個猜測:

query="SELECT ao.price, bo.price FROM OrderBook " + 
    "INNER JOIN askorder ao " + 
    "INNER JOIN bidorder bo " + 
    "WHERE ao.ORDERID = :askOrderId" 
+0

感謝亞歷克斯,但我仍然得到與該查詢相同的例外,但我認爲它比我有更正確的。 – zobbo

+0

@zobbo所以嘗試註釋除第一個'query =「SELECT ao.price,bo.price FROM OrderBook」'之外的所有行。如果它的工作取消註釋下一行。依此類推。 – Alex

+0

'SELECT bo.price FROM手持訂單bo'工作時,下一行'INNER JOIN AskOrder AO ON OrderBook.ASKORDERID_ORDERID = ao.ORDERID'失敗。自從原創失敗後,我編輯了第一行。試圖現在解決第二條線。 – zobbo