Last active
March 26, 2024 10:11
-
-
Save Crydust/cdcab06779cfacd7453e to your computer and use it in GitHub Desktop.
convert jpa TypedQuery to hibernate hql (=jpql)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @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