How to fix org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory in hibernate 6

I am migrating spring framework from 5.* to 6. I am getting error : hql is missing in ‘org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory’. How to fix this?

using imports in my project :
import org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory;
import org.hibernate.hql.spi.QueryTranslator;
import org.hibernate.hql.spi.QueryTranslatorFactory;

Error is hql package is missing in ‘org.hibernate.hql’

1 Like

Post the full stack trace.

using imports in my project :
import org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory;
import org.hibernate.hql.spi.QueryTranslator;
import org.hibernate.hql.spi.QueryTranslatorFactory;

Error is hql package is missing in ‘org.hibernate.hql’

1 Like

These contracts have been changed. Why do you need to parse queries manually?

You can look into org.hibernate.internal.AbstractSharedSessionContract#interpretHql how this works in the new Hibernate version.

I’m also using these imports in a method that extracts sql from CriteriaBuilder (builder.getQueryString()):

public static String extractNativeSql(String hql, EntityManager em) {
        QueryTranslator queryTranslator = new ASTQueryTranslatorFactory().createQueryTranslator(
                EMPTY, hql, EMPTY_MAP, em.unwrap(SessionImplementor.class).getFactory(), null
        );
        queryTranslator.compile(EMPTY_MAP, false);
        return queryTranslator.getSQLString();
}

It is done so that I can perform manipulations on sql before calling em.createNativeQuery(...) with manipulated sql.

Any alternatives in Hibernate 6?

1 Like

Why do you need to do that in the first place? What sort of manipulations are you doing?

I also tried to have an access to this, but I’m already on hibernate 6. I need the query conversion, because I have a special query result counter logic (an estimator which falls back to the precise count if below a threshold) made in SQL, but I need the actual SQL for it and a HQL/JPQL doesn’t work.

Take a look at how Blaze-Persistence implements the SQL extraction: https://github.com/Blazebit/blaze-persistence/blob/main/integration/hibernate6-base/src/main/java/com/blazebit/persistence/integration/hibernate/base/HibernateExtendedQuerySupport.java#L192

Instead of doing this estimate stuff, I would recommend you to maybe consider using bounded counting instead though: Blaze Persistence - Criteria API for JPA backends

we’re allowing IN operator to take more than 2000 entries, to have our whole operation done in one query. we’re also using Blaze Persistence.