Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 448
Li Ho Yin Avatar asked Jun 08 '11 08:06

Li Ho Yin


2 Answers

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.

like image 167
Karol S Avatar answered Sep 24 '22 14:09

Karol S


If you only want to know how your JPQL or Criteria Query gets translated to the SQL dialect of your database you can enable fine grained logging in the persistence xml and then look into your log files.

The property name and value depends on your JPA implementation. Here is an example of the relevant part of persistence.xml for EclipseLink:

<properties>     <property name="eclipselink.logging.level" value="FINEST"/> </properties> 
like image 35
Matt Handy Avatar answered Sep 23 '22 14:09

Matt Handy