Java 使用hibernate JPA CriteriaQuery的select with in子句出现异常

Java 使用hibernate JPA CriteriaQuery的select with in子句出现异常,java,hibernate,criteria-api,Java,Hibernate,Criteria Api,我有这个实体: @Entity(name="TestEntity") @Table(name = "TestTable") public class TestEntity { private Long id; ... some fields ... private List<TestEntity> children; @ManyToMany(cascade = CascadeType.ALL) @Column public List

我有这个实体:

@Entity(name="TestEntity")
@Table(name = "TestTable")
public class TestEntity {
    private Long id;
    ... some fields ...
    private List<TestEntity> children;


    @ManyToMany(cascade = CascadeType.ALL)
    @Column
    public List<TestEntity> getChildren() {
        return children;
    }

    public void setChildren(List<TestEntity> children) {
        this.children = children;
    }
}

无效的JPQL。在其他集合中不能有集合字段。您必须在某个集合中有一个元素。此外,您的元素必须与不属于您的集合的元素的类型相同。阅读JPA实现文档中的JPQL

您是否尝试了select testen。。。在你的蟾蜍或什么东西来检查语法是的,在正常情况下它是正确的。但是当我想在childrenI身上选择时出现了一个例外我曾经遇到过一个类似的问题我的情况是孩子们根本没有被提取,或者被提取为null或空集合经过多次调查后发现,如果你在glassfish身上试过,它可能有效,也可能无效,在我的例子中,它是有效的,但当我在weblogic上部署它时,出现了混乱,原因是*库和JPA版本实现*特别是当你转换到JPA 2.0 2.1这样的更高版本时,你在旧版本上做了工作,我希望这会给你一个新的体验,如何找到联接表并用它联接所选的表?在JPQL中找不到任何联接表,它是JPA实现的内部概念。在类中使用字段。您可以很容易地按照所有JPQL文档在JPQL或标准中进行连接。然后对集合TestEntity的元素设置WHERE限制。例如,我的实现是hibernate。但是看看生成的sql。它包含所有正确的联接。但是where is dotAs中的列名已经说过,您需要进行连接,然后使用元素的字段。在本例中,您将使用id作为In的前缀。对哪个实现或什么SQL不感兴趣,因为这在JPA规范中。
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<?> query = builder.createQuery(theClass);
Root from = query.from(theClass);
Path objectPath = from.get("children");
predicate = objectPath.in(1, 2, 3, 4, 5, 6);
Predicate ands = builder.and(predicate to array);
query.select(from).where(ands);
  Caused by: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement : 

  select  testentity0_.id as id1_0_, testentity0_.code as code2_0_, testentity0_.description as descript3_0_, testentity0_.mainType as mainType4_0_ from TestTable testentity0_ cross join TestTable_TestTable children1_, TestTable testentity2_ where testentity0_.id=children1_.TestTable_id and children1_.children_id=testentity2_.id and (. in (1 , 2 , 3 , 4 , 5 , 6)) [42001-182]