mellowdeep

createnativequery

Oct 31st, 2016
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.85 KB | None | 0 0
  1.           Query nativeQuery = em.createNativeQuery("SELECT e.employee_id," +
  2.           "e.first_name," + "e.last_name," + "e.department_id As id," +
  3.           " e.salary " + " FROM employees AS e " + " JOIN departments AS d " +
  4.           " ON d.department_id = e.department_id " +
  5.           " WHERE d.name = 'Research and Development'" +
  6.           " ORDER BY e.salary ASC, e.first_name DESC");
  7.          
  8.           List<Object[]> employeeList = nativeQuery.getResultList();
  9.               for  (Object[] object : employeeList) {
  10.               int pk = (int) object[0];
  11.                int departmentPk =(int) object[3];
  12.                Employee employee = em.find(Employee.class, pk);
  13.                Department department = em.find(Department.class, departmentPk);
  14.               System.out.println(String.format("%s %s from %s - $%.2f",
  15.               employee.getFirstName(), employee.getLastName(),
  16.               department.getName(), employee.getSalary()));
  17.           }
Advertisement
Add Comment
Please, Sign In to add comment