Monday, July 28, 2014

Debugging Hibernate

Debugging Hibernate application tends to be tricky as there is a time lag between application issuing a calls on hibernate api and hibernate issuing SQL to the database.We will look into various strategies to look into how to debug hibernate.
Configuration
Enable show_sql property in the hibernate configuration file. In your hibernate.cfg,xml put the following
<!-- Log out all the sql that hibernate is issuing to datbase.
             This is very useful for debugging -->
        <property name="show_sql">true</property>


This will flush all the sql that hibernate issues to databae. For example in ((Hibernate Introduction with Annotation)) example you will see in the console
Hibernate: insert into Student (STUDENT_ID, name) values (null, ?)
Enable format_sql to format the generate sql. However this will take more space. Change in hibernate.cfg.xml
<!-- Format the logged sql -->
        <property name="format_sql">true</property>


In the console
Hibernate:
    insert
    into
        Student
        (STUDENT_ID, name)
    values
        (null, ?)


To understand the origin of SQL, hibernate can put comments which can be enabled by putting the following property in hibernate.cfg.xml
<!-- Show comment -->
        <property name="use_sql_comments">true</property>


In the console
Hibernate:

    /* insert com.oyejava.Student */
     insert
        into
            Student
            (STUDENT_ID, name)
        values
            (null, ?)


Enabling log4j
Enabling logging level in log4j.properties can provide more information about the internal of application. Copy the sample log4j.properties from hibernate core download at etc folder. Run the application and you will tons of information about internals of hibernate. Tweak the level in log4j.properties. Enabling log4j.logger.org.hibernate.type at debug level will show the bind parameters. Do the following in log4j.properties. The following line is already there, you have to uncomment it.
log4j.logger.org.hibernate.type=TRACE
In the console
Hibernate: insert into Student (STUDENT_ID, name) values (null, ?)
16:04:15,440 DEBUG StringType:133 - binding 'oyeJava' to parameter: 1




Database Logs

One more way to look into generated SQL is look into your database logs. Almost all databases provide logs where the fired SQL can be seen. To see how to enable Postgres logs follow this.



Debugging Source
The hibernate download comes with the source code. The src folder in the download has all the source. For hard to solve problems you can start digging into the source code itself. In eclipse you can do it as follows:
  • Go to Project properties by right clicking on project name in navigator view and than click on Properties.
  • Go to Java Build Path -> Source
  • Click on Link Source
  • In the Linked Folder location and pick the location of hibernate source till src
  • In the folder name give a name which does not conflicts with your present source location.
  • Click on Finish and than Ok. You can now put break point in the application and can go the hibernate code.

1 comment:

  1. Great post, I was searching something like this to solve my problem, thanks!

    ReplyDelete