Java tutorial
package com.liyablieva.jaxws.dao; import com.liyablieva.jaxws.model.Employee; import org.hibernate.SQLQuery; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.context.support.SpringBeanAutowiringSupport; import java.util.Collections; import java.util.List; @Repository("EmployeeDAO") public class EmployeeDAOImpl extends SpringBeanAutowiringSupport implements EmployeeDAO { @Autowired private SessionFactory sessionFactory; @Override @Transactional(value = "transactionManager") public int putEmployee(String department, String name, String gender, String license) { System.out.println("In put employee DAO"); System.out.println("Department = " + department + ", name = " + name + ", gender = " + gender + ", license = " + license); int departmentId = getDepartmentId(department); String sql = "INSERT INTO EMPLOYEE(EMPLOYEE_ID, EMPLOYEE_NAME, EMPLOYEE_GENDER, EMPLOYEE_LICENSE, DEPARTMENT_ID)" + " VALUES ((SELECT max(employee_id) FROM employee)+1,:name,:gender,:license,:department_id);"; // SHOULD NOT USE (SELECT max(employee_id) FROM employee)+1 but GenerationType.SEQUENCE does not work! SQLQuery query = sessionFactory.getCurrentSession().createSQLQuery(sql); query.setParameter("name", name); query.setParameter("gender", gender); query.setParameter("license", license); query.setParameter("department_id", departmentId); int result = query.executeUpdate(); System.out.println(license); System.out.println("result = " + result); return result; } @SuppressWarnings("unchecked") @Override @Transactional(value = "transactionManager") public List<Employee> getEmployees(String department) { System.out.println("In get employees DAO"); int departmentId = getDepartmentId(department); System.out.println("Department = " + department + ", departmentId = " + departmentId); List<Employee> employees = Collections.emptyList(); String sql = "SELECT * FROM employee WHERE department_id = :department_id"; SQLQuery query = sessionFactory.getCurrentSession().createSQLQuery(sql); query.addEntity(Employee.class); query.setParameter("department_id", departmentId); employees = query.list(); return employees; } @Override public int getDepartmentId(String department) { System.out.println("In getDepartment method."); String[] departments = department.split(";"); int parentId = 0; Object id; for (int i = 0; i < departments.length; i++) { String sql = "SELECT DEPARTMENT_ID FROM DEPARTMENT WHERE department_name = :department_name AND parent_id = :parent_id"; SQLQuery query = sessionFactory.getCurrentSession().createSQLQuery(sql); query.setParameter("department_name", departments[i]); query.setParameter("parent_id", parentId); id = query.uniqueResult(); //uniqueResult returns null if object was not found if (id == null) { throw new IllegalArgumentException("Department was not found!"); } //cast if id != null parentId = (int) id; } System.out.println("Department id = " + parentId); return parentId; } }