With Criteria API, reusing Predicates with subqueries we are getting this error:
Already registered a copy: org.hibernate.query.sqm.tree.select.SqmSubQuery@4416e18d
Without subqueries, we are getting other error
org.hibernate.sql.ast.SqlTreeCreationException: Could not locate TableGroup
Sample:
CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<MyEntity> cq = cb.createQuery(MyEntity.class); Root<MyEntity> root = cq.from(MyEntity.class); List<Predicate> predicates = getPredicates(cb,cq, root, "1"); cq.select(root).where(predicates.toArray(Predicate[]::new)); TypedQuery<MyEntity> q = entityManager.createQuery(cq); long count = count(entityManager, predicates);
Same code in hibernate 5 works fine.