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>
<!-- List of XML mapping files -->
<mapping resource="Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Employee.java file
package com.tkhts;
public class Employee {
private int id;
private String firstName;
private String lastName;
private int salary;
public Employee(String firstName,
String lastName, int salary){
this.firstName = firstName;
this.lastName = lastName;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
Employee.hbm.xml file
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.tkhts.Employee"
table ="EMPLOYEES_TABLE">
<id name="id" column="ID" type="int">
<generator class="native"></generator>
</id>
<property name="firstName"
column="first_name"
type="string"></property>
<property name="lastName" column="last_name"
type="string"></property>
<property name="salary" column="salary"
type="int"></property>
</class>
</hibernate-mapping>
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(fName, lName, salary);
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();
}
}
}
Output On Console
SLF4J: Failed to load class
"org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation
(NOP) logger implementation
SLF4J: See
http://www.slf4j.org/codes.php#StaticLoggerBinder
for further details.
Hibernate: insert into EMPLOYEES_TABLE
(first_name, last_name, salary) values (?, ?, ?)
Table in Mysql