【问题标题】:JPA use IN clause with objectsJPA 对对象使用 IN 子句
【发布时间】: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


【解决方案1】:

当您收到来自MYSQL 而不是Hibernate 的错误时,您可以尝试找出实际生成的查询。为此,请使用p6spy 之类的代理调用程序,然后一切都应该清楚了。检查 p6spy site。当您这样做时,请尝试自己调用此类生成的 SQL 并尝试修复它。当我在使用 JPA 的 join fetches 和其他东西时遇到一些麻烦时,我使用了这种方法。对诊断此类问题非常有帮助。

【讨论】:

  • 即使我在学习 jpql 时也必须遵循相同的方法。好建议。来自 e 的支持。
【解决方案2】:

好的,我找到了问题,然后找到了答案。感谢@Antoniossss,事实是我查看了查询的错误部分。

错误在这里:c.idDernierStatut IN ...

事实上,这部分是一个外键。当你想搜索它时,你必须将它视为一个对象。所以正确的形式是c.idDernierStatut.idListeStatutsCommande IN来获取ID。

无论如何,谢谢你们俩的时间!

【讨论】:

  • 很好,你可以解决。它被称为路径表达式。在 JPQL 中,我们处理的是对象而不是数据库表,这就是为什么您必须使用对象属性进行遍历的原因。快乐学习。
  • @PrasadKharkar 我实际上正在学习它,这是一条有用的建议。谢谢