【发布时间】:2025-12-31 10:10:11
【问题描述】:
我在使用 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)'
【问题讨论】:
-
你是用hibernate实现的吗?
-
@PrasadKharkar 不,我不是,我用括号检查了问题,也不是……
-
我会说
.ID有问题而不是INclause -
@Antoniossss 我同意,但是然后呢?正如您在错误中看到的那样,ID 在那里
-
我刚刚测试了 MYSQL 的响应消息,如果出现错误,“near”将从错误点开始。不知道怎么回事强硬。
标签: java mysql jakarta-ee jpa jpql