Reputation: 2942
UPDATE
How to print a query string with parameter values when using Hibernate This contains answer marked as right, but it is from 2009! Hibernate logging config depends on version. This written just few lines down. I tried this and many others but they are not work for me.
If anybody can help, please ask what should I add to description. Thanks.
END UPDATE
The problem is I cannot set logging in manner, when all SQL parameter values are shown. Please help to do this.
I searched about that and find that I should set logging property
org.hibernate.type to ALL
I tried to set it in log4j.properties file:
log4j.logger.org.hibernate.type=ALL
I tried to set it in log4j.xml
<logger name="org.hibernate.type">
<level value="trace"/>
</logger>
I know log4j reads xml config for sure, as I get errors in log if I put some wrong tags in log4j.xml.
Unfortunately this not works. May be pom.xml file broken. All I was able to get - SQL commands written to console, but only with hibernate.cfg.xml
<property name="hibernate.show_sql">true</property>
If I set this org.hibernate.SQL property in log4j.xml it not works:
<logger name="org.hibernate.SQL">
<level value="debug"/>
</logger>
As I found on web - logging setup for hibernate depends on its version. On my project I cannot set hibernate version, but can play with dependencies on my local workstation. Here relevant data from my pom.xml
<!--hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.8.Final</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>4.1.0.Final</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>3.2.0.Final</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.8.Final</version>
<scope>compile</scope>
<type>jar</type>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Hibernate Spatial for mysql. This will include Hibernate Spatial Core and JTS -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-spatial</artifactId>
<version>4.0.1</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- custom type mappings for java 8 time classes -->
<!--hibernate -->
<dependency>
<groupId>ca.gfisystems</groupId>
<artifactId>hibernate-utils</artifactId>
<version>1.0</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- c3p0 used for postgresql connection -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!-- hibernate c3p0 connection pooling -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>4.1.8.Final</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- logback -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.3</version>
</dependency>
Upvotes: 3
Views: 11011
Reputation: 654
I have also faced same problem. Tried many solutions but system was not showing parameters with SQL statements.
At-last I did following steps, which resolved my issue.
Changed log4j library version from version (1.2.16) to (1.2.11) for just testing
Added following lines in log4j.properties
log4j.logger.org.hibernate=INFO
log4j.logger.org.hibernate.SQL=DEBUG
log4j.logger.org.hibernate.type=TRACE
And in hibernate.cfg.xml
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
And then that problem is resolved
Main issue, which I analyzed is library version (1.2.16). May be, there is some other configuration on version (log4j - 1.2.16) to show parameters.
Upvotes: 3