Hibernate - SPLessons

Hibernate log4j

Chapter 32

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Hibernate log4j

Hibernate log4j

shape Description

The functionality of Hibernate log4j is providing the logging packages written in Java, Hibernate log4j has been hatched to other languages also like c, ruby, Perl. More frameworks will use the support of log4j for logging information details, Hibernate also uses the log4j package, while Developing an application with a log4j developer need to add the jar files. Like hibernating jar files, log4j. jar and slf4j. jar. Logging will be possible by Hibernate log4j in two ways as follows.

shape Examples

Following is an example to perform Hibernate log4j with hibernate with xml file. Following are the required files to deal with an application.

  1. Employee.java
  2. StoreData.java
  3. hibernate.cfg.xml
  4. employee.hbm.xml
  5. log4j.xml


public class Employee {
private int id;
private String name;
private float salary;

public Employee() {}
public Employee( String name, float salary) {
	this.name = name;
	this.salary = salary;
public int getId() {
	return id;
public void setId(int id) {
	this.id = id;
public String getName() {
	return name;
public void setName(String name) {
	this.name = name;
public float getSalary() {
	return salary;
public void setSalary(float salary) {
	this.salary = salary;


Here created the class Employee by giving variables id, name, salary. This keyword is used to refer current object. Set and Get methods are a pattern of data encapsulation. Instead of accessing class member variables directly, one can define get methods to access these variables, and set methods to modify them.


import org.hibernate.*;
import org.hibernate.cfg.*;

public class StoreData {
public static void main(String[] args) {
	Configuration cfg=new Configuration();
	SessionFactory factory=cfg.buildSessionFactory();
	Session session=factory.openSession();
	Transaction tx=session.beginTransaction();
	session.save(new Employee("John",38000000));
	session.save(new Employee("Rock",48000000));
	System.out.println("record successfully persisted");

Here main method has been written, Application acquires session objects from Session Factory. SessionFactory is for the most part arranged as Singleton in application, SessionFactory stores produce SQL statements and other mapping metadata that Hibernate utilizes at runtime.

<?xml version='1.0' encoding='UTF-8'?>  
<!DOCTYPE hibernate-mapping PUBLIC   
 “//Hibernate/Hibernate Mapping DTD 3.0//EN”    
 <class name=”SPlessons.Employee”table=”emp845”>   
 <id name=”id”>  
 <generator class=”native”></generator>    
 <property name=”name”></property>   
 <property name=<”salary”></property>    

The generator class subelement of id utilized to produce the unique identifier for the objects of persistence class. There are numerous generator classes characterized in the Hibernate Framework. All the generator classes actualizes the org.hibernate.id.IdentifierGenerator interface.


<?xml version='1.0' encoding='UTF-8'?>  
<!DOCTYPE hibernate-configuration PUBLIC   
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"   
<property name="hbm2ddl.auto">update</property>  
 <property name="dialect"<org.hibernate.dialect.MySQLDialect>/property&>    
<property name="connection.url">jdbc:mysql://localhost:3306/test_db</property>    
 <property name="connection.username">root</property>    
 <property name="connection.password:">root</property>;    
 <property name="connection.driver_class">com.mysql.jdbc.Driver</property>    
 <mapping resource="employee.hbm.xml">    

Properties Description
hibernate.connection.driver_class The JDBC driver class.
hibernate.dialect This property makes Hibernate generate the suitable SQL for the picked database.
hibernate.connection.url The JDBC URL to the database instance.
hibernate.connection.username The database username.
hibernate.connection.password The database password.
hibernate.connection.pool_size Limits the number of connections waiting in the Hibernate database connection pool.
hibernate.connection.autocommit Allows autocommit mode to be used for the JDBC connection.


<?xml version=”1.0” encoding=”UTF-8”?>   
<!DOCTYPE log4j:configuration SYSTEM “log4j.dtd”>  
<log4j:configuration xmlns:log4j=”http://jakarta.apache.org/log4j/”   
 <appender name=”CONSOLE” class=”org.apache.log4j.ConsoleAppender”>    
 <layout class=”org.apache.log4j.PatternLayout”>    
 <param name=”ConversionPattern”value=”[%d{dd/MM/yy hh:mm:ss:sss z}] %5p %c{2}: %m%n” /”    
 <appender name=”ASYNC” class=”org.apache.log4j.AsyncAppender”>  
 <appender-ref ref=”CONSOLE” >    
 <appender-ref ref=”FILE” />   
 <appender name=”FILE”class=”org.apache.log4j.RollingFileAppender”>   
 <param name=”File” value=”C:/SPlessons.log”/>    
 <param name=”MaxBackupIndex” value=”100” />  
 <layout class=”org.apache.log4j.PatternLayout”>    
 <param name=”ConversionPattern”value=”[%d{dd/MM/yy hh:mm:ss:sss z}] %5p %c{2}: %m%n” />
 <category name=”org.hibernate”>    
 <priority value=”DEBUG”/>  
 <category name=”java.sql”>   
 <priority value=”debug” >   
 <priority value=”INFO” />    
 <appender-ref ref=”FILE” />   

Log4j is a straightforward and adaptable logging system. Logging furnishes the engineer with point by point setting for application disappointments. With log4j it is conceivable to empower logging at runtime without altering the application double. The log4j bundle is outlined so that these announcements can stay in sent code without bringing about a substantial execution cost.

Following message will be printed in the console of the IDE.

In the database files have stored , if developer update the names then it will be updated.


shape Key Points

  • Log4j will have logging package completely developed in JAVA.
  • Most of the developers will use xml file for effective logging.
  • Log4j Property file also will be kept along with configuration file and mapping file.