Following is an example to perform Hibernate log4j with hibernate with xml file. Following are the required files to deal with an application.
- Employee.java
- StoreData.java
- hibernate.cfg.xml
- employee.hbm.xml
- log4j.xml
Employee.java
[java]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;
}
}[/java]
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.
StoreData.java
[java]import org.hibernate.*;
import org.hibernate.cfg.*;
public class StoreData {
public static void main(String[] args) {
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory factory=cfg.buildSessionFactory();
Session session=factory.openSession();
Transaction tx=session.beginTransaction();
session.save(new Employee("John",38000000));
session.save(new Employee("Rock",48000000));
tx.commit();
session.close();
System.out.println("record successfully persisted");
}
}[/java]
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.
employee.hbm.xml
[java]
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
“//Hibernate/Hibernate Mapping DTD 3.0//EN”
“http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”>
<hibernate-mapping>
<class name=”SPlessons.Employee”table=”emp845”>
<id name=”id”>
<generator class=”native”></generator>
</id>
<property name=”name”></property>
<property name=<”salary”></property>
</class>
</hibernate-mapping>
[/java]
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.
hibernate.cfg.xml
[java]
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<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">
</session-factory>
</hibernate-configuration>
[/java]
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. |
log4j.xml
[java]
<?xml version=”1.0” encoding=”UTF-8”?>
<!DOCTYPE log4j:configuration SYSTEM “log4j.dtd”>
<log4j:configuration xmlns:log4j=”http://jakarta.apache.org/log4j/”
debug=”false”>
<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” /”
</layout>
</appender>
<appender name=”ASYNC” class=”org.apache.log4j.AsyncAppender”>
<appender-ref ref=”CONSOLE” >
<appender-ref ref=”FILE” />
</appender>
<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” />
</appender>
<category name=”org.hibernate”>
<priority value=”DEBUG”/>
</category>
<category name=”java.sql”>
<priority value=”debug” >
</category>
<root>
<priority value=”INFO” />
<appender-ref ref=”FILE” />
</root>
</log4j:configuration>
[/java]
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.
Output:
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.