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

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

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

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

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

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

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

Sign up to vote on this title
UsefulNot useful