Best practice to migrate DetachedCriteria to JPA Criteria API

We face issues by migrating a huge legacy project to the JPA Criteria API in Hibernate 6 due to massive usage of the removed DetachedCriteria. So far i did not find any information about how to migrate the DetachedCriteria into the JPA Criteria API. Maybe there is just no “simple” migration available. If anyone has done the migration of the DetachedCriteria already, maybe he can share some hints/ideas how they have done it. Any help is highly appreciated.

The use of DetachedCriteria can usually be wrapped into custom classes e.g.

DetachedCriteria query = DetachedCriteria.forClass(Employee.class);
query.add(Property.forName("name").eq("John Doe"));

can be encapsulated as e.g. Specification

Specification<Employee> spec = (root, query, cb) -> {
    return cb.equal( root.get("name"), "John Doe" );
};

Where Specification is an interface like this.

interface Specification<T> {
    Predicate toPredicate(Root<T> queryRoot, CriteriaQuery<T> query, CriteriaBuilder cb);
}

which you can call like:

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Employee> query = cb.createQuery(Employee.class);
Root<Employee> root = query.from(Employee.class);
query.where( specification.toPredicate(root, query, cb) );
TypedQuery<Employee> typedQuery = entityManager.createQuery(query);
List<Employee> list = typedQuery.getResultList();