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

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 '?' 
Advertisement

One thought on “EclipseLink: How to get the SQL translated with the arguments for a Query?

  1. it works perfectly! i’ve been looking with this kind of solution for a long time now. Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s