Hibernate debugging--locating the source of the query

Source: Internet
Author: User

This is the article I translated in Importnew, starting in Importnew, where links are updated regularly.

Why does hibernate sometimes generate a specified SQL query in a part of the program? This problem is difficult to understand immediately, especially when dealing with code that is not written by us.

This article shows how to configure the logs to produce Hibernate query operations. Use these logs and tips to find out why and where these specified queries are being executed.

Hibernate query Log format

Hibernate's built-in query log format is as follows:

Select/* Load Your.package.Employee */This_.code, ... from Employee This_ where this_.employee_id=? TRACE [email protected]:06:02  basicbinder-binding parameter [1] as [number]-1000
Why can't hibernate record the final query log?

It is important to note that hibernate only records the prepared statements (prepared statement) and parameters that are sent from it to JDBC. Prepare the statement using "? "As placeholders for query parameters, the actual values of these parameters are recorded below the prepared statement.

These prepared statements are different from the SQL statements that are eventually sent to the database, and hibernate cannot be logged for these final query operations. This occurs because hibernate only knows the prepared statements and parameters that it sends to JDBC, and the actual queries are built by JDBC and sent to the database.

In order to generate a log of the actual query, such a tool like LOG4JDBC is essential and will not discuss how to use LOG4JDBC.

How to find the original query operation

The documented query above contains a callout that, in most cases, identifies a starting query statement. If a query is caused by loading, then the callout is/*load your.entity.name*/. If it is a named query, then the callout contains the name of the query.

If it is a query that corresponds to many deferred loads, the callout contains the name of the corresponding class and the property value that raised the action.

Set the query log for hibernate

In order to obtain the query log, you need to add the following tags to the session factory configuration file:

<bean id= "Entitymanagerfactory" > ...  <property name= "Jpaproperties" >  <props>      <prop key= "Hibernate.show_sql" >true</prop >      <prop key= "Hibernate.format_sql" >true</prop>      <prop key= "Hibernate.use_sql_comments" >true</prop>  </props></property>

The example above shows the configuration of the Spring entity management factory. Here is an explanation of some of the tags:

    • Show_sql: Activates the query log function.
    • Format_sql: Output SQL gracefully.
    • Use_sql_comments: Adds an explanatory callout.

In order to record the parameter information of the query statement, log4j or relative information is needed.

<logger name= "Org.hibernate.type" >    <level value= "Trace"/></logger >
If none of the above functions can be run

In most cases, the annotations created by use_sql_comments are sufficient to identify the start of the query. But if this is not enough, we can identify the entity returned by the query associated with the data table name and set the breakpoint in the returned entity constructor.

If an entity has no constructors, we can create a constructor and set the breakpoint in the super () function call.

@Entitypublic class Employee {public    Employee () {        super ();//Put the breakpoint here    } ...    }

After setting a breakpoint, jump to the Debug interface containing the program stack information and execute it from start to finish. This will appear in the call stack where the query operation is created.


Hibernate debugging--locating the source of the query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.