Skip to content

Instantly share code, notes, and snippets.

@prameshbajra
Created May 3, 2024 03:00
Show Gist options
  • Save prameshbajra/c57eb60d488d43490dc710c769643bd8 to your computer and use it in GitHub Desktop.
Save prameshbajra/c57eb60d488d43490dc710c769643bd8 to your computer and use it in GitHub Desktop.
CriteriaQuery to SQL print.
org.hibernate.Session session = entityManager.unwrap(org.hibernate.Session.class);
org.hibernate.query.Query<?> query = session.createQuery(criteriaQuery);
SharedSessionContractImplementor sessionImplementor = entityManager.unwrap(SharedSessionContractImplementor.class);
String sql = sessionImplementor.getFactory()
.getQueryPlanCache()
.getHQLQueryPlan(query.getQueryString(), false, Collections.emptyMap())
.getSqlStrings()[0];
System.out.println("Generated SQL: " + sql);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment