beautypg.com

Modifying the employeedao.java file – HP Integrity NonStop H-Series User Manual

Page 391

background image

Modifying the

EmployeeDao.java

File

Modify the

EmployeeDao.java

file (under the

com.hp.empinfo.service

package), to

change the JDBC queries to Hibernate queries for the insert, delete, and retrieve operations, using
the following steps:

1.

Instantiate the Hibernate SessionFactory as shown:

private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

2.

Add the following import statements to enable the Hibernate SessionFactory:

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;

3.

Change all the database queries to use the Hibernate SessionFactory.

After modification, the

EmployeeDao.java

file should appear as:

package com.hp.empinfo.service;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import com.hp.empinfo.service;

public class EmployeeDao {

private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

public Employee getDetail(int empid) throws SQLException {
Session session = sessionFactory.openSession();
List l = session.createQuery(
"from Employee where emp_id=" + empid).list();
Iterator i = l.iterator();
Employee employee = i.next();
session.flush();
return employee;
}

public void insertDetail(int empid, String firstname, String lastname,
int age, String email) throws SQLException {

Session session = sessionFactory.openSession();

Employee emp = new Employee();
emp.setEmpid(empid);
emp.setFirstname(firstname);
emp.setLastname(lastname);
emp.setAge(age);
emp.setEmail(email);

session.save(emp);
session.flush();
}
public String deleteEmployee(int empid) {

Example of Integrating Hibernate into Spring

391

This manual is related to the following products: