Explain Codes LogoExplain Codes Logo

How to view the SQL queries issued by JPA?

sql
jpa-logging
hibernate
eclipselink
Alex KataevbyAlex Kataev·Aug 8, 2024
TLDR

Swiftly see the SQL queries issued by JPA by tuning your JPA provider's configuration. In case of Hibernate, here's a simple tweak. The hibernate.show_sql property set to true versus false is like turning on a verbose log output. Settle for more human-friendly SQL with hibernate.format_sql:

<property name="hibernate.show_sql" value="true"/> <property name="hibernate.format_sql" value="true"/>

Fair warning: this setup could affect performance. Use it when you're debugging or optimizing!

Get the most of JPA logging

More detailed SQL logging

All SQL logs are not made equal! By boosting the logging depth, you can extract more information for optimizations.

Logging with EclipseLink? Enable parameter logs as follows:

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

With FINE level, EclipseLink is the town crier, announcing all its intricate operations.

Hibernate Settings

Hibernate's logging powers can be harnessed to analyze detailed data like criteria and parameter bindings:

<property name="hibernate.type" value="trace"/> <property name="hibernate.format_sql" value="true"/>

With trace, Hibernate transforms into an open book, revealing statement placeholders and binding value details.

Harnessing the power of logging frameworks

Publish and monitor logs using frameworks like Logback or Log4j. They can connect the dots between your application and the carried-out SQL queries:

<!-- Hibernate's got secrets. Logback will spill them all --> <logger name="org.hibernate.SQL" level="DEBUG"/> <logger name="org.hibernate.type.descriptor.sql" level="TRACE"/>

Or for EclipseLink's secrets:

<Logger name="org.eclipse.persistence.session.file" level="FINE"/> <Logger name="org.eclipse.persistence.logging.sql" level="FINE"/>

Sharpshooting with logs

  • Silencer mode: Less chatty, more focused logging by applying filters in your logging library.
  • Selective squealing: Keep logs slim by logging only the queries.
  • IDE Spy: Make your IDE a monitoring station by integrating logging frameworks and viewing logs in real-time.

Custom logging for detailed insights

If you're in the EclipseLink camp, you can use its API to fetch SQL strings at runtime:

DatabaseQuery databaseQuery = ((JpaEntityManager) entityManager).createQueryDatabaseQuery(yourJpqlQuery); String sqlString = databaseQuery.getSQLString();

Believe me, it's like the agent sharing a live report of its mission!

Hibernate customization

Ride with Hibernate? Control the level of SQL detail by setting log categories:

<property name="hibernate.type.descriptor.sql.BasicBinder" value="TRACE"/>

This reveals the secrets of values bound to query placeholders. Enjoy the SQL transparency!

Performance conscious logging

Remember though, this level of detail can put a strain on performance. So, adjust your settings according to the operation environment.

IDE Integration

Ever wish to keep an eye on logs without leaving your IDE? Eclipse, IntelliJ IDEA, and NetBeans, support log level customization within the IDE.

Server-side view without database access

Sometimes, you don't have access to the database server logs. JPA logging provides surprising insights:

  • Hibernate statistics: Enable Hibernate's built-in statistics for a summary of the SQL performance.
  • EclipseLink Profiler: A performance profiler can analyze your queries. EclipseLink brings its own profiler for this task.