2014-01-14 25 views
0

例如,我將此查詢部分作爲預定義的temp。來自字符串的JPA/HQL本地查詢

String temp = "select st from xxxx st where " 

後來還有if else操作符。

if (c>v)temp += "yyy is null"; 
else temp += "yyy = 2"; 
Query query = em.createNativeQuery(temp); 

堆棧跟蹤異常:

javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query 

也許我做錯了?

+0

如果某些表和模式是預定義的,而EM是EntityManager的 – undefined

+0

如果要添加到'WHERE'條款,你應該使用條件'和'。 – elias

+2

如果執行「原生」查詢或JPQL查詢,請記住它們;您當前創建的JPQL尚未調用爲本機。 – DataNucleus

回答

0

在哪裏語句中的多個條款被and分離:

else temp += " and yyy = 2" 

而且原生SQL查詢不使用SELECT子句中的別名來選擇他們使用*所有列。

String temp = "select * from xxxx st where " 
+0

misstyped,對不起 – undefined

+0

@undefined在問題中錯誤地鍵入了一些內容?或者實際的代碼?你的問題解決了嗎? –

+0

有問題,添加了我錯過的所有內容 – undefined

0

試試這樣,只是把yyy變量if條件裏面,因爲它是在你的查詢重複

String temp = "select st from xxxx st where " 

if (c>v) 
    temp += "yyy is null"; 
else 
    temp += "yyy = 2" 

Query query = em.createNativeQuery(temp); 
+0

misstyped,對不起 – undefined

0

我想這是因爲你忘了yyy 或之前st.標識也許因爲你只是選擇st但應該選擇st.*

它應該是select st.* from xxxx st where st.yyy is null

請與跟蹤Hibernate屬性show_sql = true生成的SQL,並張貼在這裏