Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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

· Java Zone
Free Resource

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

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

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

Published at DZone with permission of Michael Schnell, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}