show
hide

Java Hibernate Tutorial Part 2

In this video tutorial I am explaining how to select , update and delete a record using the hibernate framework that are the basic CRUD operations create, read, update and delete.


I have also described in detail the difference between session. load() and session.get() methods and where to use load method and where to use the get method by giving a beautiful explaination.


I have also described the property hbm2ddl used for creating the database table using hibernate framework directly by using the settings from the hibernate mapping file.


Program Code


1.Hibernate.cfg.xml







oracle.jdbc.driver.OracleDriver
jdbc:oracle:thin:@localhost:1521:XE
org.hibernate.dialect.Oracle10gDialect
sameer
sameer

false

true
true
true

update





        
Employee.java
import java.io.Serializable;
public class Employee implements Serializable{
    private int id ;
    private String name;
    private long mobile;
    private String email;
    public Employee(){}
    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 long getMobile() {
        return mobile;
    }
    public void setMobile(long mobile) {
        this.mobile = mobile;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
   }

        
Emp.hbm.xml
        




    








        
InsertEmp.java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class InsertEmp {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Configuration cfg = new Configuration();
        cfg.configure(“Hibernate.cfg.xml”);
        SessionFactory sf = cfg.buildSessionFactory();
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();
        Employee emp = new Employee();
        emp.setId(2);
        emp.setName(“peter”);
        emp.setMobile(888);
        emp.setEmail(“peter@gmail”);
        s.save(emp);
        s.flush();
        tx.commit();
        s.close();
    }
}
        
SelectEmp.java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class SelectEmp {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Configuration cfg = new Configuration();
        cfg.configure(“Hibernate.cfg.xml”);
        SessionFactory sf = cfg.buildSessionFactory();
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();
        Employee emp = (Employee)s.get(Employee.class, new Integer(11));
        System.out.println(emp.getId());
        System.out.println(emp.getName());
        System.out.println(emp.getMobile());
        System.out.println(emp.getEmail());
        s.close();
    }
}

        
UpdateEmp.java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class UpdateEmp {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Configuration cfg = new Configuration();
        cfg.configure(“Hibernate.cfg.xml”);
        SessionFactory sf = cfg.buildSessionFactory();
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();
        Employee emp = new Employee();
        emp.setId(9);
        emp.setName(“bradd pitt”);
        emp.setMobile(111111);
        emp.setEmail(“bradpitt@gmail.com”);
        s.saveOrUpdate(emp);
        s.flush();
        tx.commit();
        s.close();
    }
}
        
DeleteEmp.java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Delemp {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Configuration cfg = new Configuration();
        cfg.configure(“Hibernate.cfg.xml”);
        SessionFactory sf = cfg.buildSessionFactory();
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();
        Employee emp = (Employee)s.get(Employee.class, new Integer(9));
        emp.setMobile(0);
        s.delete(emp);
        s.flush();
        tx.commit();
        s.close();
    }
}

        
Press Esc to close