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.
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.