HP Integrity NonStop J-Series User Manual

Page 390

Advertising
background image

a.

public Employee getDetail(int empid) throws SQLException

b.

public void insertDetail(int empid, String firstname, String

lastname, int age, String email) throws SQLException

c.

public String deleteEmployee(int empid)

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 javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import com.hp.empinfo.domain.EntityEmployee;
import com.hp.empinfo.domain.Employee;

public class EmployeeDao {

private EntityManagerFactory entityManagerFactory;

public void setEntityManagerFactory(
EntityManagerFactory entityManagerFactory) {
this.entityManagerFactory = entityManagerFactory;
}

public Employee getDetail(int empid) throws SQLException {
EntityManager em = entityManagerFactory.createEntityManager();
EntityTransaction t = em.getTransaction();
t.begin();
List<EntityEmployee> l = em.createQuery(
"from EntityEmployee where emp_id=" + empid).getResultList();
Iterator<EntityEmployee> i = l.iterator();
EntityEmployee entityEmployee = i.next();

Employee employee = new Employee();
employee.setEmpid(entityEmployee.getEmp_id());
employee.setFirstname(entityEmployee.getFirst_name());
employee.setLastname(entityEmployee.getLast_name());
employee.setAge(entityEmployee.getAge());
employee.setEmail(entityEmployee.getEmail());
em.flush();
t.commit();
return employee;
}

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

EntityManager em = entityManagerFactory.createEntityManager();
EntityTransaction t = em.getTransaction();
t.begin();
EntityEmployee entityEmployee = new EntityEmployee(empid, firstname,
lastname, age, email);
em.persist(entityEmployee);
em.flush();
t.commit();
}

public String deleteEmployee(int empid) {

EntityManager em = entityManagerFactory.createEntityManager();
EntityTransaction t = em.getTransaction();
t.begin();

390 Integrating JPA with Hibernate into Spring

Advertising
This manual is related to the following products: