How do I configure OpenJPA SQL logging?

2019-04-04 11:05发布

问题:

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

回答1:

<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.

Logging documentation



回答2:

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


回答3:

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



回答4:

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>


回答5:

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)



标签: openjpa