ClassCastException occurs when I am trying the Blaze Persistence documentation sample

167 Views Asked by At

I'm trying the sample code in the document '1.5. First criteria query' section. The code is below,

CriteriaBuilder<Cat> cb = cbf.create(em, Cat.class, "c")
    .where("c.age").betweenExpression("5").andExpression("10")
    .where("SIZE(c.kittens)").geExpression("2")
    .orderByAsc("c.name")
    .orderByAsc("c.id");

And when I add the following code ClassCastException occurs.

List<Cat> results = cb.getResultList();

I created the github public repository, so you can check the code here.

Does anyone know where I am wrong?

I created the project with Spring Boot.

Any help would be appreciated. Thank you.

2

There are 2 best solutions below

1
Christian Beikov On BEST ANSWER

The problem is that your Cat entity has a kittens field which is not a plural attribute, but an integer. Change it to @OneToMany Set<Cat> kittens and the query will work. Unfortunately the problem is on the Hibernate side i.e. Hibernate does not print a proper error message but instead fails with the exception.

1
Denis Rozhko On

add logs pls next time they are always helpful

I recommend u to run cb.getResultList() in debug and understand what comes