JPQL,搜索值是IN还是NULL

时间:2015-04-16 08:58:21

标签: mysql sql java-ee jpa jpql

我使用JPQL时遇到问题。

这是我的问题:

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")

1 个答案:

答案 0 :(得分:0)

当您使用t.aColumn时,内部联接将在后台使用。试试这个

SELECT [columns] FROM Table t left join t.aColumn ac
WHERE [conditions] AND ac.aSubColumn IN ([values])
添加实际查询后

更新

@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")