What is the OpenJPA configuration to view SQL query executed in a database? I would like to view the query with all parameters executed in log or console instead of viewing the JPQL query
5 Answers
<property name="openjpa.Log" value="SQL=Trace" />
Enables logging of all SQL statements, minus parameter values.
<property name="openjpa.ConnectionFactoryProperties" value="PrintParameters=true" />
Enables logging of SQL parameters.

- 3,830
- 1
- 18
- 16
If you're using log4j, you can setup your log4j.properties file as follows, which will display both the native SQL query and any parameters:
log4j.rootLogger=WARN, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-5p %t %d{ISO8601} %l - %m%n
log4j.category.openjpa.jdbc.SQL=TRACE

- 5,067
- 7
- 34
- 39
-
The configuration you've posted will not log SQL parameters. – Rick May 29 '12 at 13:10
-
It actually does log SQL parameters. See the following example output: INSERT INTO TCATALOG (UIDPK, CATALOG_CODE, DEFAULT_LOCALE, MASTER, NAME) VALUES (?, ?, ?, ?, ?) [params=(long) 200001, (String) catalog133835591682111, (String) en_US, (int) 0, (String) Test Catalog] – Templar May 30 '12 at 05:32
-
1Fair enough. This behavior depends on the version of OpenJPA you are using. @See for additional details -- https://issues.apache.org/jira/browse/OPENJPA-1678 – Rick May 30 '12 at 12:52
To configure Open JPA for Log4J, you need to do the following in persistence xml
Open JPA Configurations {'property name=”openJpa.Log” value=”log4j”'}
Log4j properties for Open JPA configs
log4j.logger.openjpa.Query=TRACE log4j.logger.openjpa.jdbc.SQL=TRACE
http://openjpa.apache.org/builds/1.0.1/apache-openjpa-1.0.1/docs/manual/ref_guide_logging_log4j.html

- 79
- 5
Hi I want to add running persistance.xml file
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="EHS_PU">
<jta-data-source>mysqlDataSource</jta-data-source>
<class>com.ap.entity.EHSDo</class>
<class>com.ap.entity.EventDo</class>
<properties>
<property name="openjpa.Log" value="log4j" />
<property name="openjpa.ConnectionFactoryProperties" value="PrintParameters=true" />
</properties>
</persistence-unit>
</persistence>

- 1,207
- 6
- 27
- 55
In addition to Rick's answer, there is also the "openjpa.ConnectionFactory2Properties" property for connection factories used for unmanaged connections. (more details here: https://openjpa.apache.org/builds/1.2.3/apache-openjpa/docs/ref_guide_conf_openjpa.html#openjpa.ConnectionFactory2Properties)

- 35
- 6