difference between hibernate.show_sql & spring.jpa.show_sql?

Viewed 272

I couldn't find any question which clearly explains about exact differences between hibernate.show_sql and spring.jpa.show_sql. That's why I am posting this question. Could you please anyone explain to me what are the differences between these two and when to use what?

As I know, when an application doesn't support spring & JPA and they are using only Hibernate, there is the only possible to use hibernate.show_sql. When an application supports Spring & JPA it's better to use spring.jpa.show_sql and no need of specifying the hibernate.show_sql. Correct me if something wrong in my understanding?

I have seen an application where both of these are configured in the application.properties, that's where I got the doubt.

1 Answers

hibernate.show_sql and spring.jpa.show_sql result in the same if you are using Hibernate and uses a Hibernate interal logging mechanism to print the SQL statements.

spring.jpa.show_sql is just a generic property that would work with any JPA implementations.

But I prefer using a Logging Framework to print the SQL statements.

In Spring Boot you can achieve this by setting these loggers:

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

The second line also prints the bind variables.

These levels also work with non Spring Boot applications. For example if you use Hibernate standalone simply add Log4J and a Log4J configuration.

Related