How to View Actual SQL Queries Executed by Hibernate with Parameter Values

Question

How can I see the actual SQL queries executed by Hibernate, including the values of the parameters?

Answer

When working with Hibernate, you may want to see the actual SQL queries that are sent to the database, including the parameter values that replace placeholders (like `?`). The default behavior of Hibernate is to show a parameterized SQL representation, but it is also possible to log the queries in a way that reveals the actual SQL being executed.

import org.hibernate.EmptyInterceptor;

public class SQLInterceptor extends EmptyInterceptor {
    @Override
    public void onPrepareStatement(String sql) {
        System.out.println("SQL: " + sql);
        super.onPrepareStatement(sql);
    }
}

Causes

  • Hibernate shows SQL queries with parameter placeholders but not the parameter values by default.
  • The `show_sql` property merely logs the SQL statement structure without parameter values.

Solutions

  • Use Hibernate's logging capability along with a simple interceptor or an additional library like `Hibernate's SQL Logging` capabilities to capture the executed SQL statements with bound parameter values.
  • Consider using a debugger tool like Hibernate Profiler which displays detailed SQL execution information, including parameters.

Common Mistakes

Mistake: Setting the wrong logging level may not display the actual SQL queries.

Solution: Ensure you have set the logging level correctly to capture SQL details, often `DEBUG` is required for detailed logs.

Mistake: Not including necessary dependencies for logging libraries.

Solution: Check to make sure you have the logging framework (like log4j or slf4j) configured properly in your Hibernate project.

Helpers

  • Hibernate SQL logging
  • View actual SQL Hibernate
  • Hibernate print SQL with parameters
  • Hibernate query logging
  • Show actual SQL Hibernate

Related Questions

⦿Understanding the Difference Between Integer.toString() and new Integer(i).toString() in Java

Discover the key differences between Integer.toStringi and new Integeri.toString in Java and learn when to use each method for converting int to String.

⦿How to Properly Set JAVA_HOME on Windows 7 for Java Development

Learn how to set the JAVAHOME environment variable on Windows 7 and fix common errors related to Java installations.

⦿How to Exclude Specific Fields from Gson Serialization Without Using Annotations

Learn how to exclude specific fields from Gson serialization using ExclusionStrategy without annotations improving flexibility with regex.

⦿How to Recursively Delete Directories in Java

Learn how to delete directories and their contents recursively in Java with detailed explanations and code examples.

⦿How to Retrieve the First Element from a List or Set in Java?

Learn how to efficiently get the first element from a List or Set in Java with stepbystep examples and best practices.

⦿How to Retrieve an Element from a Set in Java Using Custom Equality?

Understand why Javas Set doesnt allow direct retrieval of elements and learn how to implement a workaround.

⦿Understanding the Difference Between Interface and @interface in Java

Explore the differences between Java interfaces and annotations interface with examples and clarifications on their use cases.

⦿How to Execute Code After Spring Boot Application Starts

Learn how to run code after your Spring Boot application has started monitoring directories effectively and ensuring proper context initialization.

⦿How to Use Java Generics to Enforce a Class and Interface Constraint Together?

Learn how to use Java generics to restrict a class to extend a specific class and implement an interface simultaneously.

⦿How to Resolve 'Illegal Key Size or Default Parameters' Exception in Java?

Learn how to fix the Illegal key size or default parameters exception in Java with practical solutions and insights for Cipher initialization issues.

© Copyright 2025 - CodingTechRoom.com