SQL Zone is brought to you in partnership with:

Michael Schnell is living in Hamburg (Germany) and works as a Project Manager, Architect and Senior Java Developer. Michael is a DZone MVB and is not an employee of DZone and has posted 13 posts at DZone. You can read more from them at their website. View Full User Profile

EclipseLink: How to get the SQL translated with the arguments for a Query?

09.18.2013
| 7122 views |
  • submit to reddit

The documentation at the EclipseLink Wiki is not very helpful:http://wiki.eclipse.org/EclipseLink/FAQ/JPA#How_to_get_the_SQL_for_a_Query.3F

It says: “To get the SQL translated with the arguments you need a DatabaseRecord with the parameter values.“.

// BEGIN Snippet from the Wiki
Session session = em.unwrap(JpaEntityManager).getActiveSession();
DatabaseQuery databaseQuery =
((EJBQueryImpl)query).getDatabaseQuery();
String sqlString = databaseQuery
.getTranslatedSQLString(session,
recordWithValues);
// END Snippet from the Wiki

But where to get the recordWithValues variable? Actually the solution is hard to find, but simple: databaseQuery.getTranslationRow()

// This does the trick...
String sqlString = databaseQuery
.getTranslatedSQLString(session,
databaseQuery.getTranslationRow());
// ... to replace the '?' 
Published at DZone with permission of Michael Schnell, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)