简体   繁体   English

使用 hibernate JPA CriteriaQuery 选择带有 in 子句的异常

[英]Exception on select with in clause using hibernate JPA CriteriaQuery

I have this entity :我有这个实体:

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

And I want to create a search criteria with the inner children field with something like this :我想用内部子字段创建一个搜索条件,如下所示:

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);

But I have this exception但我有这个例外

  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]

Invalid JPQL.无效的 JPQL。 You can't have a collection field "IN" some other collection.你不能有一个集合字段“IN”一些其他集合。 You have to have an element "IN" some collection.你必须有一个元素“IN”一些集合。 Also your element has to be the same type as the elements of the collection "IN" (which yours isn't either).此外,您的元素必须与集合“IN”的元素类型相同(您的元素也不是)。 Read up on JPQL in the docs of your JPA implementation在 JPA 实现的文档中阅读 JPQL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM