1

I would like to know if it is possible to retrieve the (Native) SQL Query, with parameter values, from a TypedQuery. The code would be something like:

public String getNativeSQLString(){
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class);
    Root<Foo> root = criteriaQuery.from(Foo);

    criteriaQuery.select(root);
    criteriaQuery.where(getPredicatesArray());
    TypedQuery<GhSummary> typedQuery = entityManager.createQuery(criteriaQuery);

    // magic to transform typedQuery to Native SQL

    return nativeSql;
}

An example of return would be: "SELECT * FROM foo WHERE id = 2 AND name IN ('name1', 'name2') ORDER BY name DESC".

I'm aware that using String sqlQueryString = new BasicFormatterImpl().format(typedQuery.unwrap(Query.class).getQueryString()); I'm able to retrieve the HQL SELECT * FROM foo WHERE id =:param0 AND name IN (:param1, :param2) ORDER BY name DESC, but it does not contain the values.

Thanks in advance.

igortorati
  • 23
  • 3

1 Answers1

0

I hope log4j configuration is enough for that purpose. https://www.mkyong.com/hibernate/how-to-display-hibernate-sql-parameter-values-log4j/

sjy
  • 99
  • 8