I am migrating to Hibernate 5.2 and since createCriteria is deprecated, we plan to rewrite the queries to JPA-style. I am facing an issue (java.lang.IllegalArgumentException: Not an entity) where the same code is working with hibernate criteria method.
Working with hibernate criteria
Criteria criteria = getSession().createCriteria(className);
for (Map.Entry<String, Object> entry : matchingCriteria.entrySet()) {
criteria.add(Expression.eq(entry.getKey(), entry.getValue()));
}
List objsMatchingCriteria = criteria.list();
Getting "java.lang.IllegalArgumentException: Not an entity" for JPA criteria
CriteriaBuilder cb = getSession().getCriteriaBuilder();
CriteriaQuery cq = cbuilder.createQuery(refClassName);
Root root = cquery.from(refClassName);
cq.select(root);
List<Predicate> predicates = new ArrayList<>();
for (Map.Entry<String, Object> entry : matchingCriteria.entrySet()) {
predicates.add(cb.equal(root.get(entry.getKey()), entry.getValue()));
}
cq.where(predicates.toArray(new Predicate[predicates.size()]));
List objsMatchingCriteria =
getSession().createQuery(cq).getResultList();
Can you let me know what is wrong with the JPA code
Update This problem is seen when we have base class (abstract) and the actual entity class is concrete child class. Earlier with hibernate criteria, even though I pass the base class, there is no problem in retrieving the results.
But after migrating to JPA criteria, I am not able to pass the base class. Can this be managed in JPA.
Thanks in advance.