Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

Is it possible in Hibernate to print generated SQL queries with real values instead of question marks?

How would you suggest to print queries with real values if it is not possible with Hibernate API?

1 Answer

0 votes
by (40.7k points)

First, you must enable logging for the below categories:

Two different log categories and log levels to log executed SQL statements and their parameters which is used by Hibernate are explained below:

  • For the org.hibernate.SQL category, SQL statements should be written as a DEBUG message.
  • For the org.hibernate.type.descriptor.sql category, bind parameters should be logged in with log level TRACE.

In the log configuration, you can activate & deactivate them without depending on each other. 

 The following code snippet will explain log4j configuration which will activate both:

log4j.logger.org.hibernate.SQL=debug

# To log the SQL statements

log4j.logger.org.hibernate.type.descriptor.SQL=trace

#To log the JDBC parameters to the query.

 Here, if you observe the first one is equivalent to hibernate.show_sql=true legacy property. Whereas, the second one prints the bound parameters among other things.

Related questions

0 votes
1 answer
asked Jul 26, 2019 in Java by Ritik (3.5k points)
0 votes
1 answer
0 votes
1 answer
asked Dec 4, 2020 in SQL by Appu (6.1k points)

Browse Categories

...