2016-10-03 38 views
0

我試圖做到這一點選擇:JPA,春Webservice的選擇與NOT IN和IN

SELECT c FROM Incident c 
WHERE c.incidentID IN 
    ( 
    SELECT DISTINCT d.incidentID FROM TagIncident d WHERE tagName IN (d.tagName=?1) 
    AND d.incidentID NOT IN 
    (SELECT a.incidentID FROM TagIncident a WHERE tagName IN (a.tagName=?2))  
) 

在我與JPA /彈簧系統,我發現了錯誤:

"HTTP Status 500 - Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: An exception occurred while creating a query in EntityManager:" 

是我在語法上做錯了嗎? 我在我的數據庫(HANA)上測試過它,它工作正常。

感謝您的幫助!

編輯更多的錯誤日誌

我最新的嘗試是:

SELECT c FROM Incident c WHERE c.incidentID IN 
(SELECT DISTINCT d.incidentID FROM TagIncident d WHERE d.tagName IN 
(d.tagName=?1) AND d.incidentID NOT IN 
(SELECT a.incidentID FROM TagIncident a WHERE a.tagName IN (a.tagName=?2))) 

編輯

Exception Description: Syntax error parsing [SELECT c FROM Incident c WHERE c.incidentID IN (SELECT DISTINCT d.incidentID FROM TagIncident d WHERE d.tagName IN (d.tagName=?1) AND d.incidentID NOT IN (SELECT a.incidentID FROM TagIncident a WHERE a.tagName IN (a.tagName=?2)))]. [117, 131] 
The expression at index {0} is not a valid expression. [215, 229] 
The expression at index {0} is not a valid expression.; nested exception is java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: Exception Description: Syntax error parsing [SELECT c FROM Incident c WHERE c.incidentID IN (SELECT DISTINCT d.incidentID FROM TagIncident d WHERE d.tagName IN (d.tagName=?1) AND d.incidentID NOT IN (SELECT a.incidentID FROM TagIncident a WHERE a.tagName IN (a.tagName=?2)))]. [117, 131] 
The expression at index {0} is not a valid expression. [215, 229] 
The expression at index {0} is not a valid expression.] with root cause Local Exception Stack: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.6.0.v20150309-bf26070): org.eclipse.persistence.exceptions.JPQLException Exception Description: Syntax error parsing [SELECT c FROM Incident c WHERE c.incidentID IN (SELECT DISTINCT d.incidentID FROM TagIncident d WHERE d.tagName IN (d.tagName=?1) AND d.incidentID NOT IN (SELECT a.incidentID FROM TagIncident a WHERE a.tagName IN (a.tagName=?2)))]. [117, 131] 
The expression at index {0} is not a valid expression. [215, 229] 
The expression at index {0} is not a valid expression. 

最新嘗試:

List<String> list_add_tags = new ArrayList<String>(); 
List<String> list_remove_tags = new ArrayList<String>(); 

// creating custom sql_query 
String sql_query = "SELECT c FROM Incident c WHERE c.incidentID IN (SELECT DISTINCT(d.incidentID) FROM TagIncident d WHERE d.tagName IN (:add_tags) AND d.incidentID NOT IN (SELECT a.incidentID FROM TagIncident a WHERE a.tagName IN (:remove_tags)))"; 

TypedQuery<Incident> query = em.createQuery(sql_query, Incident.class); 

query.setParameter("add_tags", list_add_tags); 
query.setParameter("remove_tags", list_remove_tags); 

return query.getResultList(); 

仍然不起作用。 =(

錯誤:

You have attempted to set a value of type class java.util.ArrayList for parameter add_tags with expected type of class java.lang.String 
+0

哪些JPA提供商您使用的? Hibernate,EclipseLink,OpenJPA還是其中一個鮮爲人知的DataNucleaus,Toplink,ObjectDB?在Hibernate中,您可以指定列表,如章節4.6.17.5 JPA規範中的示例所示,但用他們自己的話來說,他們認爲他們支持它作爲獎勵功能。也許你應該嘗試去除參數周圍的括號?官方的例子確實意味着你不應該需要它們。 – coladict

+0

EclipseLink 2.6.0,從列表中刪除了IN的括號,它的工作原理,通過這樣做我能理解什麼?我如何檢查我的JPA版本? –

回答

1

通常我在LY使用本機查詢,因爲我能更容易測試,但試試這個:

SELECT c FROM Incident c 
WHERE c.incidentID IN 
    ( 
    SELECT DISTINCT d.incidentID FROM TagIncident d WHERE tagName IN :at 
    AND d.incidentID NOT IN 
    (SELECT a.incidentID FROM TagIncident a WHERE tagName IN :rt)  
) 

這應該與query.setParameter("tag", theListOfTags)工作。請注意,5.0.7之前的Hibernate版本在括號中帶有參數的語法問題。

空列表也會產生語法錯誤。

JPA規範顯示出他們的這個例子作爲有效的語法,所以應該支持任何JPA提供商:

SELECT e 
FROM Employee e 
WHERE TYPE(e) IN :empTypes 
+0

標籤是不同的,標籤是我不想在最後選擇的東西,這是我想在第二次選擇一些標籤。我會嘗試沒有括號。 謝謝! –

+0

它不必被稱爲'標籤'。這只是使用命名參數,因此您不必使用位置並調用'setParameter'兩次,或在具有大量參數的複雜查詢中跟蹤位置。我做的最大的修正是從那裏刪除'[ad] .tagName =?1',因爲你正在與'IN(值列表)'部分進行比較。 – coladict

+0

你是對的!這是一個列表!我會盡力的,謝謝! –

0

嘗試使用此JPA查詢

SELECT c FROM Incident c, TagIncident t 
WHERE c.incidentID = t.incidentID 
AND t.tagName = ?1 
AND t.tagName != ?2 

此外,如果你啓用休眠日誌記錄,那麼你就可以看到生成的查詢,看他們是否在工作外部SQL程序

logging.level.org.hibernate=DEBUG 
+0

該SQL將導致不同的結果。 =( 它不等價,我在什麼地方在Bean中設置這個Logging? –

+0

如果您使用的是spring引導,您可以將此屬性放入'application.properties'文件中。 – 11thdimension