Finally, we will create our application class with the main() method to run the application. We will use this application to save few Employee's records and then we will apply CRUD operations on those records.
import java.util.List;
import java.util.Date; import java.util.Iterator; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; publicclassManageEmployee{ privatestaticSessionFactory factory; publicstaticvoid main(String[] args){ try{ factory=newAnnotationConfiguration() . configure(). //addPackage("com.xyz") //add package if used. addAnnotatedClass(Employee.class). buildSessionFactory(); }catch(Throwable ex){ System.err.println("Failed to create sessionFactory object."+ex); thrownewExceptionInInitializerError(ex); } ManageEmployee ME =newManageEmployee(); /* Add few employee records in database */ Integer empID1 =ME.addEmployee("Zara","Ali",1000); Integer empID2 =ME.addEmployee("Daisy","Das",5000); Integer empID3 =ME.addEmployee("John","Paul",10000); /* List down all the employees */ ME.listEmployees(); /* Update employee's records */ ME.updateEmployee(empID1,5000); /* Delete an employee from the database */ ME.deleteEmployee(empID2); /* List down new list of the employees */ ME.listEmployees(); } /* Method to CREATE an employee in the database */ publicInteger addEmployee(String fname,String lname,int salary){ Session session =factory.openSession(); Transactiontx=null; Integer employeeID =null; try{ tx= session.beginTransaction(); Employee employee =newEmployee(); employee.setFirstName(fname); employee.setLastName(lname); employee.setSalary(salary); employeeID=(Integer) session.save(employee); tx.commit(); }catch(HibernateException e){ if(tx!=null) tx.rollback(); e.printStackTrace(); }finally{ session.close(); } return employeeID; } /* Method to READ all the employees */ publicvoid listEmployees(){ Session session =factory.openSession(); Transactiontx=null; try{ tx= session.beginTransaction(); List employees =session.createQuery("FROM Employee").list(); for(Iterator iterator = employees.iterator(); iterator.hasNext();){ Employee employee =(Employee)iterator.next(); System.out.print("First Name: "+ employee.getFirstName()); System.out.print(" Last Name: "+ employee.getLastName()); System.out.println(" Salary: "+ employee.getSalary()); } tx.commit(); }catch(HibernateException e){ if(tx!=null) tx.rollback(); e.printStackTrace(); }finally{ session.close(); } } /* Method to UPDATE salary for an employee */ publicvoid updateEmployee(IntegerEmployeeID,int salary ){ Session session =factory.openSession(); Transactiontx=null; try{ tx= session.beginTransaction(); Employee employee = (Employee)session.get(Employee.class,EmployeeID); employee.setSalary( salary ); session.update(employee); tx.commit(); }catch(HibernateException e){ if(tx!=null) tx.rollback(); e.printStackTrace(); }finally{ session.close(); } } /* Method to DELETE an employee from the records */ publicvoid deleteEmployee(IntegerEmployeeID){ Session session =factory.openSession(); Transactiontx=null; try{ tx= session.beginTransaction(); Employee employee = (Employee)session.get(Employee.class,EmployeeID); session.delete(employee); tx.commit(); }catch(HibernateException e){ if(tx!=null) tx.rollback(); e.printStackTrace(); }finally{ session.close(); } } } Finally, we will create our application class with the main() method to run the application. Here it should be noted that while creating session object we used our Interceptor class as an argument.
import java.util.List;
import java.util.Date; import java.util.Iterator; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; publicclassManageEmployee{ privatestaticSessionFactory factory; publicstaticvoid main(String[] args){ try{ factory=newConfiguration().configure().buildSessionFactory(); }catch(Throwable ex){ System.err.println("Failed to create sessionFactory object."+ex); thrownewExceptionInInitializerError(ex); } ManageEmployee ME =newManageEmployee(); /* Add few employee records in database */ Integer empID1 =ME.addEmployee("Zara","Ali",1000); Integer empID2 =ME.addEmployee("Daisy","Das",5000); Integer empID3 =ME.addEmployee("John","Paul",10000); /* List down all the employees */ ME.listEmployees(); /* Update employee's records */ ME.updateEmployee(empID1,5000); /* Delete an employee from the database */ ME.deleteEmployee(empID2); /* List down new list of the employees */ ME.listEmployees(); } /* Method to CREATE an employee in the database */ publicInteger addEmployee(String fname,String lname,int salary){ Session session =factory.openSession(newMyInterceptor()); Transactiontx=null; Integer employeeID =null; try{ tx= session.beginTransaction(); Employee employee =newEmployee(fname, lname, salary); employeeID=(Integer) session.save(employee); tx.commit(); }catch(HibernateException e){ if(tx!=null) tx.rollback(); e.printStackTrace(); }finally{ session.close(); } return employeeID; } /* Method to READ all the employees */ publicvoid listEmployees(){ Session session =factory.openSession(newMyInterceptor()); Transactiontx=null; try{ tx= session.beginTransaction(); List employees =session.createQuery("FROM Employee").list(); for(Iterator iterator = employees.iterator(); iterator.hasNext();){ Employee employee =(Employee)iterator.next(); System.out.print("First Name: "+ employee.getFirstName()); System.out.print(" Last Name: "+ employee.getLastName()); System.out.println(" Salary: "+ employee.getSalary()); } tx.commit(); }catch(HibernateException e){ if(tx!=null) tx.rollback(); e.printStackTrace(); }finally{ session.close(); } } /* Method to UPDATE salary for an employee */ publicvoid updateEmployee(IntegerEmployeeID,int salary ){ Session session =factory.openSession(newMyInterceptor()); Transactiontx=null; try{ tx= session.beginTransaction(); Employee employee = (Employee)session.get(Employee.class,EmployeeID); employee.setSalary( salary ); session.update(employee); tx.commit(); }catch(HibernateException e){ if(tx!=null) tx.rollback(); e.printStackTrace(); }finally{ session.close(); } } /* Method to DELETE an employee from the records */ publicvoid deleteEmployee(IntegerEmployeeID){ Session session =factory.openSession(newMyInterceptor()); Transactiontx=null; try{ tx= session.beginTransaction(); Employee employee = (Employee)session.get(Employee.class,EmployeeID); session.delete(employee); tx.commit(); }catch(HibernateException e){ if(tx!=null) tx.rollback(); e.printStackTrace(); }finally{ session.close(); } } } |