Hibernate 6 - Already registered a copy

Hi,

I’m migrating from hibernate 5.3.24 to hibernate 6.2.1.

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.

Please try updating to 6.2.2.Final and if the problem persists, please create an issue in the issue tracker(https://hibernate.atlassian.net) with a test case(hibernate-test-case-templates/JPAUnitTestCase.java at main · hibernate/hibernate-test-case-templates · GitHub) that reproduces the issue.