hibernate.cfg.xml file
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect </property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver </property>
<!-- Assume hibernatedb as the database name -->
<property name="hibernate.connection.url">
jdbc:mysql://localhost/hibernatedb </property>
<property name="hibernate.connection.username">
root </property>
<property name="hibernate.connection.password">
root </property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database
schema on startup -->
<property name="hbm2ddl.auto">create</property>
<!-- Use when mapping with XML mapping files -->
<!-- <mapping resource="Employee.hbm.xml"/> -->
<!-- Names the annotated entity class -->
<mapping class="com.tkhts.Employee"/>
</session-factory>
</hibernate-configuration>
Employee.java file
package com.tkhts;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity //This annotation create the table with
class name if Table annotation not used
@Table(name = "EMPLOYEES_TABLE") //This annotation create
table with table name EMPLOYEES_TABLE
public class Employee {
@Id @GeneratedValue //This annotation create primary
key and generate value automatically
@Column(name = "id")
//This annotation create column name "id"
private int id;
@Column(name = "first_name")
//This annotation create column name with first_name
private String firstName;
@Column(name = "last_name")
//This annotation create column name with last_name
private String lastName;
@Column(name = "salary")
//This annotation create column name with salary_name
private int salary;
public Employee() {}
public int getId() {
return id;
}
public void setId( int id ) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName( String first_name ) {
this.firstName = first_name;
}
public String getLastName() {
return lastName;
}
public void setLastName( String last_name ) {
this.lastName = last_name;
}
public int getSalary() {
return salary;
}
public void setSalary( int salary ) {
this.salary = salary;
}
}
TestEmployee.java file
package com.tkhts;
import org.apache.log4j.chainsaw.Main;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class TestEmployee {
private static SessionFactory sesssionFactory;
public static void main(String args[]){
sesssionFactory = new Configuration()
.configure().buildSessionFactory();
TestEmployee employ = new TestEmployee();
employ.addEmployee("abc", "xyz", 100000);
// pass EmployeeID and salary as parameter to
deleteEmployee method to delete the employee
// employ.deleteEmployee(1, 1200000);
}
public void addEmployee
(String fName,String lName, int salary){
Session session = sesssionFactory.openSession();
Transaction tx = null;
tx = session.beginTransaction();
Employee emp = new Employee();
emp.setFirstName("peter");
emp.setLastName("parker");
emp.setSalary(100000);
session.save(emp);
tx.commit();
}
/* Method to UPDATE salary for an employee */
public void deleteEmployee
(Integer EmployeeID, int salary ){
Session session = sesssionFactory.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
Employee employee =
(Employee)session.get
(Employee.class, EmployeeID);
// This method set the salary of employee
// employee.setSalary( salary );
session.delete(employee);
tx.commit();
}catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}
catch(Exception e){
if (tx!=null) tx.rollback();
e.printStackTrace();
}
finally {
session.close();
}
}
}
log4j.properties
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=F:\\log\\hibernate.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=
%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout =
org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=
%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# Root logger option
log4j.rootLogger=INFO, file, stdout
# Log everything. Good for troubleshooting
log4j.logger.org.hibernate=DEBUG
# Log all JDBC parameters
log4j.logger.org.hibernate.type=ALL
Output in hibernate.log file
44,998 DEBUG SQL:111 - insert into EMPLOYEE (first_name, last_name, salary) values (?, ?, ?)
Hibernate: insert into EMPLOYEE (first_name, last_name, salary) values (?, ?, ?)
12:30:45,011 TRACE BasicBinder:82 - binding parameter [1] as [VARCHAR] - peter
12:30:45,012 TRACE BasicBinder:82 - binding parameter [2] as [VARCHAR] - parker
12:30:45,013 TRACE BasicBinder:82 - binding parameter [3] as [INTEGER] - 100000
Table in Mysql