Skip to content

Instantly share code, notes, and snippets.

@Crydust
Last active March 26, 2024 10:11
Show Gist options
  • Save Crydust/cdcab06779cfacd7453e to your computer and use it in GitHub Desktop.
Save Crydust/cdcab06779cfacd7453e to your computer and use it in GitHub Desktop.
convert jpa TypedQuery to hibernate hql (=jpql)
// @see http://antoniogoncalves.org/2012/05/24/how-to-get-the-jpqlsql-string-from-a-criteriaquery-in-jpa/
TypedQuery<X> q = entityManager.createQuery(cq);
try {
Class<?> hibernateQueryClass = Class.forName("org.hibernate.Query");
Object hibernateQuery = q.unwrap(hibernateQueryClass);
java.lang.reflect.Method getQueryStringMethod = hibernateQueryClass.getMethod("getQueryString");
Object hql = getQueryStringMethod.invoke(hibernateQuery);
LOGGER.warn("hql = {}", hql);
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | java.lang.reflect.InvocationTargetException ex) {
ex.printStackTrace();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment