2017-01-17 167 views
0
String query = "select t from myTable t where t.idOne = " + myId 
+ " AND t.idTwo = " + myIdTwo 
+ " AND t.idThree = " + myIdThree 
+ "AND t.idFour = (select max(t.idFour) from t)"; 

因此myTable有一個列,我需要從查詢中獲取MAX id並將其用於查詢。無法在JPA中進行子查詢

最後一行給我一個錯誤:

Error while running query: An exception occurred while creating a query in EntityManager:
java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: Exception Description: Syntax error parsing [query from above]. [214, 214] An identification variable must be provided for a range variable declaration.

+1

這是JPQL還是SQL?因爲'myTable'實際上應該是JPQL中的'MyEntity'(實體類的名稱)。 – SJuan76

+0

對不起它的jpql,你是對的,它是實體類的名稱,我只是這樣命名的 –

+0

這不正是你問你的問題,但(用正確的'ORDER BY'子句)會給你結果你想要的:http://stackoverflow.com/questions/6708085/select-top-1-result-using-jpa – SJuan76

回答

3

它看起來像你犯錯。 你的模式中是否有表t?如果不是,你應該指定表名而不是別名。

String query = "select t from myTable t where t.idOne = " + myId 
+ " AND t.idTwo = " + myIdTwo 
+ " AND t.idThree = " + myIdThree 
+ "AND t.idFour = (select max(st.idFour) from myTable st)"; 
+0

謝謝你這樣做。我試圖從同一張表中選擇,但我想我必須改變別名。 –