2014-09-02 41 views
0

下面的一段代碼應該返回主鍵(idList)在DB中匹配的實體列表。帶IN子句的複合鍵

這對原始Id類型正常工作,但對於組合鍵,它沒有。請幫幫我。

Class<?> entityType = TypeToken.of(typeOfEntity).getRawType(); 
Class<?> idType = TypeToken.of(typeOfId).getRawType(); 

String idFieldName = getIdFieldName(entityManager, entityType, idType); 

CriteriaQuery<?> criteria = entityManager.getCriteriaBuilder().createQuery(entityType); 
Root<?> root = criteria.from(entityType); 
Expression<ID> expression = root.get(idFieldName); 
Predicate predicate = expression.in(idList); 

TypedQuery<?> query = entityManager.createQuery(criteria.where(predicate)); 
return (List<T>) query.getResultList(); 

我現在正在使用Hibernate,並且當我運行測試時,我從休眠日誌中獲得了以下SQL。

select * from paper_invoice paperinvoi0_ where paperinvoi0_.paymentid =?和paperinvoi0_.receipt_code =?和paperinvoi0_.usetype =?

這看起來不錯,但會引發異常: javax.persistence.PersistenceException:org.hibernate.exception.GenericJDBCException:無法執行查詢。

我不知道這裏有什麼問題。任何人都可以指出?

回答

0

有產生不正確地被取代的這樣的查詢中的EclipseLink一個錯誤:該失敗並Syntax error: Encountered "NULL"

SELECT ID, TITLE FROM BOOK WHERE ((NULL, NULL) IN ((1, 2), (2, 2))) 

。使用Hibernate生成正確的查詢

select book0_.id as id1_0_, book0_.title as title2_0_ from Book book0_ where book0_.id=? and book0_.title=? or book0_.id=? and book0_.title=? 

另請參閱this question

+0

現在你提到了這個bug,請問我可以指出這個bug(bug id或bug url)嗎? – 2014-09-03 02:56:43

+0

對不起。我在尋找,但沒有找到這個問題的錯誤報告。對我來說這顯然是一個錯誤。如果你足夠友善,你可以在這裏發佈一個bug https://bugs.eclipse.org/bugs/enter_bug.cgi。 – zbig 2014-09-03 07:58:35

+0

Bug提交:https://bugs.eclipse.org/bugs/show_bug.cgi?id = 443265 – 2014-09-04 03:25:49