How to view the SQL queries issued by JPA?

Sajee picture Sajee · Dec 6, 2010 · Viewed 253.3k times · Source

When my code issues a call like this:

entityManager.find(Customer.class, customerID);

How can I see the SQL query for this call? Assuming I don't have access to database server to profile/monitor the calls, is there way to log or view within my IDE the corresponding SQL queries issued by JPA calls? I'm going against SQL Server 2008 R2 using the jTDS driver.

Answer

axtavt picture axtavt · Dec 6, 2010

Logging options are provider-specific. You need to know which JPA implementation do you use.

  • Hibernate (see here):

    <property name = "hibernate.show_sql" value = "true" />
    
  • EclipseLink (see here):

    <property name="eclipselink.logging.level" value="FINE"/>
    
  • OpenJPA (see here):

    <property name="openjpa.Log" value="DefaultLevel=WARN,Runtime=INFO,Tool=INFO,SQL=TRACE"/>
    
  • DataNucleus (see here):

    Set the log category DataNucleus.Datastore.Native to a level, like DEBUG.