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;
}

dtd"> <hibernate-mapping> <classname="com.hibernate.empName = empName. } @Override public String toString() { return"Employee [empId=" + empId + ".0"encoding="UTF-8"?> <!DOCTYPEhibernate-mappingPUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www. } } 2)Mapping File: <?xmlversion="1.0.org/dtd/hibernate-mapping- 3. empName=" + empName + ". } publicdoublegetEmpSal() { returnempSal. } publicvoidsetEmpSal(doubleempSal) { this. empSal=" + empSal + "]".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: . publicvoidsetEmpName(String empName) { this.model.empSal = empSal.app.

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

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

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

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

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

Sign up to vote on this title
UsefulNot useful