Logging in Hibernate 4.3

You have different logging options in Hibernate and let us see some of those options quickly. Hibernate supports simple property based log enabling to supporting standard logging frameworks and even programmatically accessing log statistics.


First, you can enable showsql property in Hibernate to see the exact query being executed. You can set the property to true in the hibernate.cfg.xml XML configuration file. Please refer to http://javajee.com/lab-your-first-hibernate-43-program.


Second, a logging layer such as log4j can enable class- or package-level logging. Hibernate uses Simple Logging Façade for Java (SLF4J). SLF4J abstracts an actual logging framework such as log4j, JDK 1.4 logging, JCL, Logback etc. and can direct your logging output to that framework. To set up logging, you need the slf4j-api.jar file in your classpath, together with the JAR file for your preferred binding such as slf4j-log4j12.jar if using log4j.


Third, you can also enable live statistics by setting the hibernate.generate_statistics property in the configuration file:

<property name="hibernate.generate_statistics">true</property>


Hibernate provides SessionStatistics and Statistics interfaces in the org.hibernate.stat package to access statistics programmatically.

Sample code snippets:

SessionStatistics sessionStats = session.getStatistics();

Statistics stats = sessionFactory.getStatistics();

tx = session.beginTransaction();


logger.info("getEntityCount- "+sessionStats.getEntityCount());

logger.info("openCount- "+stats.getSessionOpenCount());

logger.info("getEntityInsertCount- "+stats.getEntityInsertCount());




Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!