Hibernate 在内部查询中使用in子句休眠异常行为

Hibernate 在内部查询中使用in子句休眠异常行为,hibernate,jpa,Hibernate,Jpa,我在Hibernate中使用JPA,今天我看到了一个奇怪的行为 以下查询工作正常: select x.fichaCaracterizacao.id from FichaParecer x where x.departamento in :departamentos 但是,如果我在内部查询中使用它,它会抛出一个异常,如下所示: select p.fichaCaracterizacao.id from FichaParecer p where 1=1 and p.id in (selec

我在Hibernate中使用JPA,今天我看到了一个奇怪的行为

以下查询工作正常:

select x.fichaCaracterizacao.id 
from FichaParecer x 
where x.departamento in :departamentos
但是,如果我在内部查询中使用它,它会抛出一个异常,如下所示:

select p.fichaCaracterizacao.id 
from FichaParecer p 
where 1=1 and 
p.id in (select x.fichaCaracterizacao.id 
         from FichaParecer x 
         where x.departamento in :departamentos)
它引发以下异常:

Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: , near line 1, column 244 [select p.fichaCaracterizacao.id from br.ufscar.siga.cadastrosgerais.entity.FichaParecer p where 1=1 and p.id in (select x.fichaCaracterizacao.id from br.ufscar.siga.cadastrosgerais.entity.FichaParecer x where x.departamento in :departamentos0_, :departamentos1_, :departamentos2_)]
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1348) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1289) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:261) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
只有当“departmentos”集合有多个项时,才会发生这种情况

Hibernate支持这个吗?还是我做错了什么


编辑:我使用的是Hibernate 4.0.1.Final

您可能使用的是Hibernate的旧版本,它要求in子句写为

where x.departamento in (:departamentos)

您还可以使用自联接来避免在列表中嵌套的

SELECT p1.fichaCaracterizacao.id 
  FROM FichaParecer p1
    INNER JOIN FichaParecer p2
  WHERE p1.id = p2.fichaCaracterizacao.id 
    AND p2.departamento in :departamentos

小心列表中的
。数据库对这些列表中的元素数量施加限制。如果要从SQL语句(子查询)生成列表,请特别小心。这是4.0.1.0的最终版本。你知道那个版本是否有这个要求吗?不知道。我想的bug影响了旧版本。对于查询,可能已经解决了这个问题,但是对于子查询,却没有解决。答案是否解决了问题?是的。谢谢我以前尝试过这样做,但它给了我另一个错误(org.hibernate.hql.ast.QuerySyntaxException:意外的ast节点:…)。但在我解决了这个问题后,我可以解决另一个问题。谢谢,但这只是一个简单的例子来说明问题。应用程序中的查询更大,无法使用此技巧解决。