select with with with子句使用hibernate CriteriaQuery的异常

时间:2014-12-14 08:34:27

标签: java hibernate jpa hql hibernate-criteria

我有这个实体:

@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;
    }
}

我想用内部子字段创建一个搜索条件,如下所示:

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]

1 个答案:

答案 0 :(得分:0)

无效的JPQL。你不能拥有一个集合字段“IN”其他一些集合。你必须有一个元素“IN”一些集合。此外,您的元素必须与集合“IN”的元素相同(您的元素也不是)。阅读JPA实现文档中的JPQL