Can I get the SQL string from a JPA Query object?

Li Ho Yin picture Li Ho Yin · Jun 8, 2011 · Viewed 39.5k times · Source

May I know how can I get the sql from a JPA query? or let's say, convert the JPA query to a SQL string? Thank you very much!

Answer

Karol S picture Karol S · Feb 9, 2016

For Eclipselink: you can extract the SQL the following way:

query.unwrap(EJBQueryImpl.class).getDatabaseQuery().getSQLString()

It works only after the query has been executed.