简体   繁体   中英

How to get SQL Query with parameter values from TypedQuery?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM