简体   繁体   中英

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. You can't have a collection field "IN" some other collection. You have to have an element "IN" some collection. Also your element has to be the same type as the elements of the collection "IN" (which yours isn't either). Read up on JPQL in the docs of your JPA implementation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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