Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate 4.3 show SQL parameters

I am using log4j2 with slf4j and hibernate 4.3.1. I am having trouble showing the parameters of the SQL queries. I did not find anywayt to show those (SQL queries are well shown but without parameters). Here is what I have specified in my persistence.xml:

<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="xxxxx" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <properties>
        <property name="hibernate.archive.autodetection" value="class" />
        <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider" />
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.use_sql_comments" value="true"/>
        <property name="hibernate.query.factory_class"
            value="org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory" />
        <property name="max_fetch_depth" value="2" />
        <property name="hibernate.jdbc.batch_size" value="100" />
    </properties>
</persistence-unit>

I have defined this in my log4j2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG" verbose="true">
    <Appenders>
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout pattern="%m%n" />
        </Console>
        <File name="MyFile" fileName="test.log">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
            </PatternLayout>
        </File>
    </Appenders>
    <Loggers>
        <logger name="org.hibernate.SQL" level="DEBUG" additivity="false">
            <AppenderRef ref="STDOUT" />
            <AppenderRef ref="MyFile" />
        </logger>
        <logger name="org.hibernate.type" level="TRACE" additivity="false">
            <AppenderRef ref="STDOUT" />
            <AppenderRef ref="MyFile" />
        </logger>
        <Root level="TRACE">
            <AppenderRef ref="STDOUT" />
            <AppenderRef ref="MyFile" />
        </Root>
    </Loggers>
</Configuration>
like image 976
Abbadon Avatar asked Nov 02 '22 05:11

Abbadon


1 Answers

First, make sure you have the log4j-core-2.0, log4j-api-2.0 and log4j-1.2-api-2.0 jars in the classpath.

Next, JBoss support for Log4J2 is fairly recent. You may need JBoss Logging 3.1.4 to be able to use Log4J2 (see JBLOGGING-94). From JBoss Logging 3.2 there will be performance improvements (see JBLOGGING-95) and you may be able to omit the log4j-1.2-api-2.0 bridge jar.

Please note that the current JBoss-Logging Beta1 (3.2.0.Beta1) has a NPE when logging: JBLOGGING-107. So having it may be necessary to tell hibernate to use the slf4j API using the org.jboss.logging.provider System Property (see org.jboss.logging.LoggerProviders).

like image 97
Remko Popma Avatar answered Nov 14 '22 01:11

Remko Popma