41

I want to output the SQL generated by EclipseLink to the console, during development. However, I could only do so using the logging level FINE. I have a complex domain model composed of many classes, so much that deployment takes a considerable ammount of time when the log verbosity is on the FINE level, since EclipseLink outputs its analysis of the whole model.

Is there a way to get the SQL without resorting to log level FINE (like Hibernate does)?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
javabeats
  • 1,082
  • 3
  • 12
  • 26

3 Answers3

64

Put the following properties in your persistence.xml:

 <property name="eclipselink.logging.level.sql" value="FINE"/>
 <property name="eclipselink.logging.parameters" value="true"/>

The latter is helpful, so that the values of the parameter are shown.

An alternative is using log4jdbc or log4jdbc-remix.

jbandi
  • 17,499
  • 9
  • 69
  • 81
26

The log generation for EclipseLink seems quite difficult to set, according to this thread.

It mentions a persistence.xml file with log level you can adapt:

<property name="eclipselink.weaving" value="static" />
<property name="eclipselink.logging.level.sql" value="FINEST" />
<property name="eclipselink.logging.level" value="FINEST" />
<property name="eclipselink.logging.level.cache" value="FINEST" />

But some other settings may be needed.

As Martin documents below, "EclipseLink/Examples/JPA/Logging" documents those properties.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 2
    Yes, it works. Thanks a lot! The new question now is - why isn't it documented somewhere? And if it is, why isn't this easy to find? ;) – javabeats Mar 04 '10 at 14:01
  • 1
    @javabeats Documentation can be found in the [EclipseLink Wiki](https://wiki.eclipse.org/EclipseLink/Examples/JPA/Logging) (first result when searching for `EclipseLink logging` there). – Martin Jul 12 '16 at 12:56
  • @Martin Thank you. I have included your comment in the answer for more visibility. – VonC Jul 12 '16 at 12:58
  • @Martin bear in mind that the question is 6 years old, not sure if this was documented at the time... – javabeats Jul 13 '16 at 14:05
  • Is there a way to do this through Spring's `application.properties` file instead? – payne Oct 27 '21 at 02:21
  • @payne eleven years later, I wouldn't know. I am not familiar with Spring's `application.properties`. That could be a good question on its own, to ask on this site. – VonC Oct 27 '21 at 05:25
14

To get the SQL for a specific Query at runtime you can use the DatabaseQuery API.

Query query = em.createNamedQuery("findMe"); 
Session session = em.unwrap(JpaEntityManager.class).getActiveSession(); 
DatabaseQuery databaseQuery = ((EJBQueryImpl)query).getDatabaseQuery(); 
databaseQuery.prepareCall(session, new DatabaseRecord());

String sqlString = databaseQuery.getSQLString();

This SQL will contain ? for parameters. To get the SQL translated with the arguments you need a DatabaseRecord with the parameter values.

DatabaseRecord recordWithValues= new DatabaseRecord();
recordWithValues.add(new DatabaseField("param1"), "someValue");

String sqlStringWithArgs = 
         databaseQuery.getTranslatedSQLString(session, recordWithValues);

Source: How to get the SQL for a Query

Tomasz
  • 5,269
  • 8
  • 56
  • 65
  • 2
    Very useful. Setting eclipse link to log all is a massive spam. This can be easily put into an Utility class and be used with precision to log the SQL only where interesting. E.g. where trying to find appropriate SQL indexes to create, and nowhere else. Thanks. – 99Sono Jun 18 '16 at 17:39
  • String sql = ((EJBQueryImpl) query).getDatabaseQuery().getTranslatedSQLString(entityManager.unwrap(JpaEntityManager.class).getSession(), new DatabaseRecord()); – Vikash Kumar Verma Oct 21 '19 at 13:08