2015-04-16 61 views
0

我有使用JPQL的問題。JPQL,搜索值是IN還是NULL

這裏是我的查詢:

SELECT [columns] FROM Table t 
WHERE [conditions] AND t.aColumn.aSubColumn IN ([values]) 
OR t.aColumn IS NULL 

所以要解釋一下,T包含一列是一個外鍵到另一個表(這樣的對象)。我試圖查看該表的某個字段是否包含某些值,但如果外鍵爲空,那麼在查詢中我們不會考慮它。我怎樣才能做到這一點?

PS:當我嘗試運行查詢時,結果爲空,因爲FK爲空,所以它在IN子句中找不到任何東西。

編輯:原來的查詢

@NamedQuery(name = "Commande.findCustom", query = "SELECT c FROM Commande c WHERE " 
      + "(c.idChargement LIKE :idChargement OR c.idChargement IS NULL) AND " 
      + "(c.codeTransporteur LIKE :codeTransporteur OR c.codeTransporteur IS NULL) AND " 
      + "(((c.dateChargementPrevu BETWEEN :dateDebut AND :dateFin) OR " 
      + "c.dateChargementPrevu IS NULL) OR " 
      + "((c.dateLivraisonPrevue BETWEEN :dateDebut AND :dateFin) OR " 
      + "c.dateLivraisonPrevue IS NULL)) AND " 
      + "(c.idDernierStatut.idListeStatutsCommande IN " 
      + "(SELECT l.idListeStatutsCommande FROM Listestatutscommande l " 
      + "WHERE l.idStatut IN :idStatut) OR c.idDernierStatut IS NULL) AND " 
      + "(c.raisonSocialeDestinataire LIKE :raisonSociale OR c.raisonSocialeDestinataire IS NULL) AND " 
      + "(c.adresseDestinataire LIKE :adresseDestinataire OR c.adresseDestinataire IS NULL) AND " 
      + "(c.codeDestinataire LIKE :codeDestinataire OR c.codeDestinataire IS NULL) AND " 
      + "(c.codePostalDestinataire LIKE :codePostal OR c.codePostalDestinataire IS NULL) AND " 
      + "(c.villeDestinataire LIKE :villeDestinataire OR c.villeDestinataire IS NULL) AND " 
      + "(c.paysDestinataire LIKE :codePays OR c.paysDestinataire IS NULL) AND " 
      + "c.idPartenaire IN :idPartenaire " 
      + "ORDER BY c.dateLivraisonPrevue DESC") 

回答

0

當您使用t.aColumn然後內部聯接在後臺使用。試試這個

SELECT [columns] FROM Table t left join t.aColumn ac 
WHERE [conditions] AND ac.aSubColumn IN ([values]) 

UPDATE增加實際查詢後使用

@NamedQuery(name = "Commande.findCustom", query = "SELECT c FROM Commande c left join c.idDernierStatut idds WHERE " 
      + "(c.idChargement LIKE :idChargement OR c.idChargement IS NULL) AND " 
      + "(c.codeTransporteur LIKE :codeTransporteur OR c.codeTransporteur IS NULL) AND " 
      + "(((c.dateChargementPrevu BETWEEN :dateDebut AND :dateFin) OR " 
      + "c.dateChargementPrevu IS NULL) OR " 
      + "((c.dateLivraisonPrevue BETWEEN :dateDebut AND :dateFin) OR " 
      + "c.dateLivraisonPrevue IS NULL)) AND " 
      + "(idds is null or idds.idListeStatutsCommande IN " 
      + "(SELECT l.idListeStatutsCommande FROM Listestatutscommande l " 
      + "WHERE l.idStatut IN :idStatut)) AND " 
      + "(c.raisonSocialeDestinataire LIKE :raisonSociale OR c.raisonSocialeDestinataire IS NULL) AND " 
      + "(c.adresseDestinataire LIKE :adresseDestinataire OR c.adresseDestinataire IS NULL) AND " 
      + "(c.codeDestinataire LIKE :codeDestinataire OR c.codeDestinataire IS NULL) AND " 
      + "(c.codePostalDestinataire LIKE :codePostal OR c.codePostalDestinataire IS NULL) AND " 
      + "(c.villeDestinataire LIKE :villeDestinataire OR c.villeDestinataire IS NULL) AND " 
      + "(c.paysDestinataire LIKE :codePays OR c.paysDestinataire IS NULL) AND " 
      + "c.idPartenaire IN :idPartenaire " 
      + "ORDER BY c.dateLivraisonPrevue DESC") 
+0

我試了一下,結果是一樣的:沒有找到匹配的結果...... – trichetriche

+0

你能後,你是真正的查詢測試?因爲如果在select子句中有't.aColumn',那麼也會強制使用內部連接。另外,您可以打印出生成的SQL來查看究竟發生了什麼。 –

+0

我編輯了我的帖子,把我原來的查詢。 – trichetriche