我面临一个问题,即使用JPA,更具体地说是使用IN子句。
我认为最好的方法是向您展示我的代码:
@NamedQuery(name = "Commande.findCustom", query = "SELECT DISTINCT [myFields] "
+ "FROM Commande c WHERE "
+ "[SomeCriterias] AND "
+ "c.ID IN (SELECT t.ID FROM SubTable t "
+ "WHERE t.IDX IN :param) AND [otherCriterias]"),
然后我从MySQL收到错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.ID FROM SubTable t1 WHERE (t1.IDX IN (168)'
我正在寻找答案但找不到任何东西......
我绑定删除一个IN子句,问题仍然是相同的(所以它不是一个双重IN使用问题)
我的:param
是一个对象列表,是我使用Object.find()
方法得到的。如您所见,它返回ID(168)。但我似乎无法找到问题...
非常感谢任何帮助,谢谢
编辑:完整查询
@NamedQuery(name = "Commande.findCustom", query = "SELECT DISTINCT c.idChargement, c.libelle, "
+ "c.codeTransporteur, c.reference, c.dateCreation, c.dateChargementPrevu, "
+ "c.dateValidationChargement, c.dateLivraisonPrevue, c.codeDestinataire, "
+ "c.raisonSocialeDestinataire, c.adresseDestinataire, c.codePostalDestinataire, "
+ "c.villeDestinataire, c.paysDestinataire, c.contactDestinataire, "
+ "c.telephoneDestinataire, c.mailDestinataire, c.poidsCommande, c.nombreColis, "
+ "c.nombreUniteManutention, c.typeUniteManutention, c.prendreRDV, c.commentaires "
+ "FROM Commande c WHERE "
+ "c.idChargement = :idChargement AND c.codeTransporteur = :codeTransporteur AND "
+ "(c.dateCreation BETWEEN :dateDebut AND :dateFin) AND "
+ "c.idDernierStatut IN (SELECT l.idListeStatutsCommande FROM Listestatutscommande l "
+ "WHERE l.idStatut IN :idStatut) AND c.raisonSocialeDestinataire = :raisonSociale AND "
+ "c.adresseDestinataire = :adresseDestinataire AND c.codeDestinataire = :codeDestinataire "
+ "AND c.codePostalDestinataire = :codePostal AND c.villeDestinataire = :villeDestinataire "
+ "AND c.paysDestinataire = :codePays")
错误信息
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.idListeStatutsCommande FROM listestatutscommande t1 WHERE (t1.IdStatut IN (168)'
答案 0 :(得分:1)
当您从MYSQL
而不是Hibernate
收到错误时,您可能会尝试找出实际生成的查询。要做到这一点,使用代理调用程序,如p6spy
,然后一切都应该清楚。检查p6spy site。当你这样做时,尝试自己调用这样生成的SQL并尝试修复它。当我使用JPA的连接提取和东西时遇到麻烦时,我使用了这种方法。非常有助于诊断此类问题。
答案 1 :(得分:0)
好的,我发现了问题,然后是答案。谢谢@Antoniossss,事实是我正在查看查询的错误部分。
错误在于:c.idDernierStatut IN ...
事实是这部分是外键。当你想要搜索它时,你必须将它视为一个对象。因此,正确的表单是c.idDernierStatut.idListeStatutsCommande IN
来获取ID。
无论如何,感谢你们两个人!