Creating the employee.hbm.xml file – HP Integrity NonStop J-Series User Manual

Page 372

Advertising
background image

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<Employee> l = session.createQuery(
"from Employee where emp_id=" + empid).list();
Iterator<Employee> 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) {
Session session = sessionFactory.openSession();
Employee employee = new Employee();
employee.setEmpid(empid);
session.delete(employee);
session.flush();
return "Employee deleted";

Creating the Employee.hbm.xml File

Create the Employee.hbm.xml (Hibernate Mapping) file in EmpInfo/src directory to map
Java class with the database entities.

To create the Employee.hbm.xml file in the EmpInfo/src directory, complete the following
steps:

1.

On the Project Explorer frame, right-click EmpInfo and select New > Other.

The New File dialog box appears.

2.

From the list of folders, select XML > XML and click Next.

372 Integrating Hibernate into Spring

Advertising
This manual is related to the following products: