In this EJB JPA Tutorial, we will make changes to the Bean Layer for Edit Employee workflow.
Remote Interface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package tube.codingexamples.ejb.statelessbean; import java.util.List; import javax.ejb.Remote; import tube.coding.examples.jpa.entity.Employee; import tube.coding.examples.jpa.entity.Job; @Remote public interface EmpJobBeanRemote { //Sample 8.08: Bean Method to add Employee public Employee addEmployee(String FName, String MName, String LName, int jobLevel, int jobid); public List<Job> queryJobs(); //Sample 8.19: Bean Method for Add Job/Modify Flow public Job addJob(String jobDesc, int minExp, int maxExp); public List<Employee> queryEmployees(); public List<Employee> queryEmployees(int jobId); //Sample 8.29: Edit Employee public Employee empFindById(String empId); public Job jobFindById(int jobId); public Employee editEmployee(String empId, String FName, String MName, String LName, int jobLevel, int jobid ); } |
Bean Implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
package tube.codingexamples.ejb.statelessbean; import java.util.List; import javax.ejb.LocalBean; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import tube.coding.examples.jpa.entity.Employee; import tube.coding.examples.jpa.entity.Job; @Stateless(mappedName = "EmpJobBean") @LocalBean public class EmpJobBean implements EmpJobBeanRemote { //Sample 8.09: Create Entiry Manager, UnitName is from persistance.xml @PersistenceContext(unitName = "JPASavingsAccount") EntityManager em; public EmpJobBean() { } //Sample 8.10: Implement the Add Employee Method public Employee addEmployee(String FName, String MName, String LName, int jobLevel, int jobid) { //8.10.1: Create Employee Instance Employee emp = new Employee(); emp.setFirstName(FName); emp.setMidName(MName); emp.setLastName(LName); emp.setJobLevel(jobLevel); //8.10.2: Generate EmpId and Set it String id = "PMA42628M"; Integer num = (int) (Math.random() * 100000); id = id.replace("42628", num.toString()); emp.setId(id); //8.10.3: Hook the Job to Employee. EM will take care of the relation Job jobToLink = em.find(Job.class, jobid); emp.setJob(jobToLink); em.persist(emp); return emp; } //Sample 8.11: Implement query jobs public List<Job> queryJobs() { String JpaSql = "Select J from Job J "; Query query = em.createQuery(JpaSql); List<Job> jobs = (List<Job>) query.getResultList(); return jobs; } //Sample 8.20: Get all Employees @Override public List<Employee> queryEmployees() { String JpaSql = "Select E from Employee E "; Query query = em.createQuery(JpaSql); List<Employee> employees = (List<Employee>) query.getResultList(); return employees; } //Sample 8.21: Add Job to DB [with 1-Many Relation] @Override public Job addJob(String jobDesc, int minExp, int maxExp) { //8.21.1: Set Fields Job job = new Job(); job.setJobDesc(jobDesc); job.setMinExp(minExp); job.setMaxExp(maxExp); //8.21.2: 1-Many Link //job.setEmployees(null); //8.21.3: Push to DB em.persist(job); return job; } //Sample 8.22: Find Employee with Specific Job and Return @Override public List<Employee> queryEmployees(int jobId) { //8.22.1: Find Job First Job job = em.find(Job.class, jobId); //8.22.2: Return Employees. [Null should be checked by the caller] //Remember! Job is a one to many entriy return job.getEmployees(); } //Sample 8.30: Find Employee & Job by Id @Override public Employee empFindById(String empId) { return em.find(Employee.class, empId); } @Override public Job jobFindById(int jobId) { return em.find(Job.class, jobId); } //Sample 8.31: Find Employee, Update, Persist public Employee editEmployee(String empId, String firstName, String midName, String lastName, int jobLevel, int jobId ) { //8.31.1: Find Employee Employee emp = empFindById(empId); //8.31.2: Update Fields emp.setFirstName(firstName); emp.setMidName(midName); emp.setLastName(lastName); emp.setJobLevel(jobLevel); emp.setJob(jobFindById(jobId)); //8.31.3: Persisit Information em.persist(emp); return emp; } } |
Categories: JavaEE-EJB-Tube