Can't avoid hibernate logging SQL to console with Spring Boot and Logback

If you set the hibernate.show_sql to true, Hibernate will simply print the SQL statement to the console (not to be confused with logging under org.hibernate.SQL). SqlStatementLogger is responsible for logging the SQL statements and its logStatement looks like:

public void logStatement(String statement, Formatter formatter) {
    if ( format ) {
        if ( logToStdout || LOG.isDebugEnabled() ) {
            statement = formatter.format( statement );
        }
    }
    LOG.debug( statement );
    if ( logToStdout ) {
        System.out.println( "Hibernate: " + statement );
    }
}

So, if you do not want to see the queries on the console, just disable the hibernate.show_sql by setting it to false or just removing it altogether. In Spring Boot, just add this to your application.properties:

spring.jpa.show-sql=false

I just wanted to share that I just noticed there is another setting which could cause org.hibernate.SQL to debug in Spring Boot JUnit tests, though you might have set

spring.jpa.show-sql=false 

and

spring.jpa.properties.hibernate.show_sql=false

...

If you set

debug=true

in your Spring application*.properties file!

This one set to true will override the show-sql setting and set it to true.

Brgds


You basically need to set 2 properties to false.

If you are using Spring boot , then set up in Application.properties as below

spring.jpa.properties.hibernate.generate_statistics=false
spring.jpa.properties.hibernate.show_sql=false

And If you are using hibernate.cfg.xml, then set up as below

<property name="hibernate.generate_statistics">false</property>
<property name="show_sql">false</property>