Press enter to see results or esc to cancel.


JAVA Hibernate CRUD Create Read Update Delete Project

Download the Project shown in Youtube video here

CRUD_Hibernate_Test.rar
  1. Project structure in Eclipse
  2. This project uses MySql database. So we need to add MySql jars in Class path.
    Since we are using Hibernate its not needed to create a database manually. Just add Hibernate Jars in Classpath.
    Download Mysql Jar here
    Download Hibernate Jars here
  3. Employee.java
    package com;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    @Entity
    public class Employee {
    	@Id
    	private int sl_no;
    	private String employee_name;
    	private String mobil_no;
    	public int getSl_no() {
    		return sl_no;
    	}
    	public void setSl_no(int sl_no) {
    		this.sl_no = sl_no;
    	}
    	public String getEmployee_name() {
    		return employee_name;
    	}
    	public void setEmployee_name(String employee_name) {
    		this.employee_name = employee_name;
    	}
    	public String getMobil_no() {
    		return mobil_no;
    	}
    	public void setMobil_no(String mobil_no) {
    		this.mobil_no = mobil_no;
    	}
    }
    
  4. Employee_insert.java
    package com;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.AnnotationConfiguration;
    public class Employee_insert {
    public static void insert_data(){
    try {
    	SessionFactory sessionfactory=new AnnotationConfiguration().configure().buildSessionFactory();
    	Session session=sessionfactory.openSession();
    	Employee obj_Employee=new Employee();
    	obj_Employee.setSl_no(2);
    	obj_Employee.setEmployee_name("Emplyee One");
    	obj_Employee.setMobil_no("111111");
    	session.save(obj_Employee);
    	session.beginTransaction().commit();
    	session.close();
    	sessionfactory.close();
          } catch (Exception e) {
    	System.out.println(e);
          }
        }
    }
    
  5. Employee_Get.java
    package com;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.AnnotationConfiguration;
    public class Employee_Get {
    public static void get_data(){
         try {
    	SessionFactory sessionfactory=new AnnotationConfiguration().configure().buildSessionFactory();
    	Session session=sessionfactory.openSession();
    	Employee obj_Employee=new Employee();
    	obj_Employee=(Employee)session.get(Employee.class,2);
    	System.out.println("Get the Employee Data -"+obj_Employee.getEmployee_name());
    	session.beginTransaction().commit(); 
    	session.close();
    	sessionfactory.close();
        } catch (Exception e) {
    		System.out.println(e);
    	}
        }
    }
    
  6. Employee_Update.java
    package com;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.AnnotationConfiguration; 
    public class Employee_Update {
    public static void update(){
    	try {
    	SessionFactory sessionfactory=new AnnotationConfiguration().configure().buildSessionFactory();
    	Session session=sessionfactory.openSession();
    	Employee obj_Employee=new Employee();
    	obj_Employee.setSl_no(2);
    	obj_Employee.setEmployee_name("Employee Two");
    	obj_Employee.setMobil_no("22222222");
    	session.update(obj_Employee);
    	System.out.println("Updated-"+obj_Employee.getEmployee_name());
    	session.beginTransaction().commit(); 
    	session.close();
    	sessionfactory.close();
    	} catch (Exception e) {
    		System.out.println(e);
    	}
    	}
    }
    
  7. Employee_Delete.java
    package com;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.AnnotationConfiguration;
    public class Employee_Delete {
       public static void delete(){
       try {
    	SessionFactory sessionfactory=new AnnotationConfiguration().configure().buildSessionFactory();
    	Session session=sessionfactory.openSession();
    	Employee obj_Employee=new Employee();
    	obj_Employee.setEmployee_name("Employee Two");
    	session.delete(obj_Employee);
    	System.out.println("Deleted  "+obj_Employee.getEmployee_name());
    	session.beginTransaction().commit(); 
            session.close();
            sessionfactory.close();
        } catch (Exception e) {
    	System.out.println(e);
         }
        }
    }
    
  8. <?xml version="1.0" encoding="UTF-8"?>
    <?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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.connection.password">root</property>
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
            <property name="show_sql">true</property>
            <property name="hbm2ddl.auto">update</property>
            <mapping class="com.Employee" />
        </session-factory>
    </hibernate-configuration>
    
  9. Employee_DAO.java

    package com;
    public class Employee_DAO {
       public static void main(String[] args) {
    	System.out.println("Insert Employee One");
    	Employee_insert.insert_data();
    	System.out.println();
    	System.out.println("Get Employee One");
    	Employee_Get.get_data();
    	System.out.println();
    	System.out.println("Update Employee One to Employee Two");
    	Employee_Update.update();
    	System.out.println();
    	System.out.println("Delete Employee Two");
    	Employee_Delete.delete();
        }
    }
    
  10. On running Employee_DAO.java as Run as Java Application. You will get the below out put.
    It will do Insert, Get, Update and Delete operations.

    Insert Employee One
    log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
    log4j:WARN Please initialize the log4j system properly.
    Hibernate: insert into Employee (employee_name, mobil_no, sl_no) values (?, ?, ?)
    
    Get Employee One
    Hibernate: select employee0_.sl_no as sl1_2_0_, employee0_.employee_name as employee2_2_0_, employee0_.mobil_no as mobil3_2_0_ from Employee employee0_ where employee0_.sl_no=?
    Get the Employee Data -Emplyee One
    
    Update Employee One to Employee Two
    Updated-Employee Two
    Hibernate: update Employee set employee_name=?, mobil_no=? where sl_no=?
    
    Delete Employee Two
    Hibernate: select employee_.sl_no, employee_.employee_name as employee2_6_, employee_.mobil_no as mobil3_6_ from Employee employee_ where employee_.sl_no=?
    Deleted  Employee Two
    
    
  11. Download the Project here,

    CRUD_Hibernate_Test