Search code examples
javahibernatehibernate-6.x

How to get SQL from Hibernate Criteria API in Hibernate 6?


I'd like to get the sql string from a CriteriaQuery using Hibernate 6.

Any ideas?


Solution

  • This was an intentional design decision by the Hibernate team in version 6 as Hibernate moved to the semantic query model. I did find a workaround to do this in the following code. It's worth noting that the format of the HQL this method returns has changed slightly since Hibernate 5:

    QuerySqmImpl<?> unwrap = entityManager.createQuery(query).unwrap(QuerySqmImpl.class);
    String queryString = unwrap.getSqmStatement().toHqlString();
    

    You can see some discussion around this in the Hibernate JIRA ticket for this issue as well.