JPA 对对象使用 IN 子句
JPA use IN clause with objects
我在使用 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子句,问题还是一样(所以不是double IN使用的问题)
我的 :param
是一个对象列表,是我使用 Object.find()
方法得到的。如您所见,它 returns 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)'
由于您从 MYSQL
而不是 Hibernate
收到错误,您可以尝试找出实际生成的查询。为此,请使用像 p6spy
这样的代理调用程序,然后一切都应该清楚了。检查 p6spy site。当你这样做时,尝试自己调用生成的 SQL 并尝试修复它。当我在使用 JPA 的连接提取和东西时遇到一些麻烦时,我使用了这种方法。对诊断此类问题很有帮助。
好的,所以我找到了问题,然后是答案。谢谢@Antoniosss,事实是我看错了查询的部分。
错误在这里:c.idDernierStatut IN ...
事实是这部分是外键。当你想在它上面搜索时,你必须把它当作一个对象来考虑。所以正确的形式是c.idDernierStatut.idListeStatutsCommande IN
来获取ID。
谢谢你们抽出宝贵的时间!
我在使用 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子句,问题还是一样(所以不是double IN使用的问题)
我的 :param
是一个对象列表,是我使用 Object.find()
方法得到的。如您所见,它 returns 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)'
由于您从 MYSQL
而不是 Hibernate
收到错误,您可以尝试找出实际生成的查询。为此,请使用像 p6spy
这样的代理调用程序,然后一切都应该清楚了。检查 p6spy site。当你这样做时,尝试自己调用生成的 SQL 并尝试修复它。当我在使用 JPA 的连接提取和东西时遇到一些麻烦时,我使用了这种方法。对诊断此类问题很有帮助。
好的,所以我找到了问题,然后是答案。谢谢@Antoniosss,事实是我看错了查询的部分。
错误在这里:c.idDernierStatut IN ...
事实是这部分是外键。当你想在它上面搜索时,你必须把它当作一个对象来考虑。所以正确的形式是c.idDernierStatut.idListeStatutsCommande IN
来获取ID。
谢谢你们抽出宝贵的时间!