Hibernate Jars Download Link :(copy and paste in

browser)
https://sourceforge.net/projects/hibernate/files/hibernate3/3.1.3/hibernate-3.1.3.zip/download

Hibernate POJI-POJO Design Pattern with Singletone
SessionFactory (using HibernateUtil):
1) Model Class:
packagecom.app.model;

publicclass Employee {

privateintempId;
private String empName;
privatedoubleempSal;

public Employee() {
super();
}

public Employee(intempId) {
super();
this.empId = empId;
}

public Employee(intempId, String empName,
doubleempSal) {
super();
this.empId = empId;
this.empName = empName;
this.empSal = empSal;
}

publicintgetEmpId() {
returnempId;
}
publicvoidsetEmpId(intempId) {
this.empId = empId;
}
public String getEmpName() {
returnempName;
}

hibernate.0"encoding="UTF-8"?> <!DOCTYPEhibernate-mappingPUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.empName = empName. } publicvoidsetEmpSal(doubleempSal) { this.empSal = empSal. publicvoidsetEmpName(String empName) { this.model. } @Override public String toString() { return"Employee [empId=" + empId + ".Employee"table="emptab1"> <idname="empId"column="eid"/> <propertyname="empName"column="ename"length="10"uniqu e="true"/> <propertyname="empSal"column="esal"precision="5"scale ="2"/> </class> </hibernate-mapping> 3)Configuration File: . } } 2)Mapping File: <?xmlversion="1.app. empName=" + empName + ". empSal=" + empSal + "]".org/dtd/hibernate-mapping- 3.dtd"> <hibernate-mapping> <classname="com. } publicdoublegetEmpSal() { returnempSal.0.

.driver_class">com .0.hbm.mysql.hibernate.password">root</p roperty> <propertyname="hibernate.connection. importcom.model.<?xmlversion="1.dialect">org.0"encoding="UTF-8"?> <!DOCTYPEhibernate-configurationPUBLIC "-//Hibernate/Hibernate Configuration DTD 3.MySQLDialect</property> <mappingresource="employee.xml"/> </session-factory> </hibernate-configuration> 4) POJI (IDao): packagecom.hbm2ddl.app.dao.connection.hibernate.auto">update</proper ty> <propertyname="hibernate.diale ct.org/dtd/hibernate- configuration-3.connection.show_sql">true</property> <propertyname="hibernate.url">jdbc:mysql:/ /localhost:3306/test</property> <propertyname="hibernate.Driver</property> <propertyname="hibernate.Employee.connection.username">root</p roperty> <propertyname="hibernate.dtd"> <hibernate-configuration> <session-factory> <propertyname="hibernate.jdbc.0//EN" "http://www.app.

app. importorg. importcom.util.hibernate.IEmployeeDao.app.hibernate.Employee. try { .publicinterfaceIEmployeeDao { publicintsaveEmployee(Employee emp). publicvoiddeleteEmployee(intempId).app.Session. } packagecom.dao.SessionFactory.app. } } 5)POJO ( DaoImpl) packagecom.Transaction. importorg.hibernate. publicvoidupdateEmployee(Employee emp).cfg. publicclassHibernateUtil { privatestaticSessionFactorysf=null. static{ sf=new Configuration(). } publicstaticSessionFactorygetSeesionFactory(){ returnsf.model. importcom. int id=-1. public Employee getEmployeeById(intempId). importcom.hibernate.configure().Configuration.HibernateUtil.buildSessionFactory().util. importorg.dao.impl. importorg.app. Transaction tx=null. publicclassEmployeeDaoHibernateImplimplementsIEmploye eDao{ @Override publicintsaveEmployee(Employee emp) { Session ses=null.

}finally{ if(ses!=null) ses. } @Override publicvoidupdateEmployee(Employee emp) { Session ses=null. } return id.commit(). try { . e.getSeesionFactory().commit().close(). } catch (Exception e) { tx. id=(Integer)ses.rollback().close().ses=HibernateUtil.begin().rollback(). tx. Transaction tx=null. tx.getSeesionFactory(). tx=ses.openSession(). tx=ses.begin(). e. try { ses=HibernateUtil. } } @Override publicvoiddeleteEmployee(intempId) { Session ses=null. }finally{ if(ses!=null) ses.update(emp).printStackTrace().beginTransaction().save(emp).beginTransaction(). tx. Transaction tx=null.openSession(). } catch (Exception e) { tx. ses. tx.printStackTrace().

getSeesionFactory(). }finally{ if(ses!=null) ses. Transaction tx=null.openSession(). tx. tx. tx=ses. ses.close().begin(). e. if(ob!=null) emp=(Employee)ob.rollback().getSeesionFactory().ses=HibernateUtil. Employee emp=null.beginTransaction().printStackTrace().delete(new Employee(empId)). tx=ses.class.empId).openSession(). Object ob=ses.beginTransaction(). }finally{ if(ses!=null) ses.get(Employee.commit(). } } 6) TEST class:- . } } @Override public Employee getEmployeeById(intempId) { Session ses=null. } catch (Exception e) { tx. } returnemp.printStackTrace(). } catch (Exception e) { tx.rollback().close(). e. try { ses=HibernateUtil.

"AAAA".dao.Employee. publicclass Test { publicstaticvoid main(String[] args) { IEmployeeDaodao=newEmployeeDaoHibernateImpl(). importcom. importcom. 8563.impl. "sdsd".36)).app.app. 523. /*dao.out. importcom.*/ /*dao.getEmployeeById(10).saveEmployee(new Employee(10.model.app.EmployeeDaoHibernateImpl.packagecom.println(emp).app.dao. System.updateEmployee(new Employee(10. } } .test.*/ Employee emp=dao.36)).IEmployeeDao.

Sign up to vote on this title
UsefulNot useful