How can I make a DAO object a property of other DAO?
Say I have an Employee object with a Department property
public class Employee {
public Department;
//setter and getters
}
I had this EmployeeDAO and DepartmentDAO interfaces with corresponding implementations
And I had DAOFactory
public abstract class DAOFactory {
// db connection instantiation here
public IEmployeeDAO getEmployeeDAO() {
return new EmployeeDAOImpl(this);
}
public IDepartmentDAO getDepartmentDAO() {
return new DepartmentDAOImpl(this);
}
}
i had a servlet where instantiate this DAOfactory
public class EmployeeController extends HttpServlet {
public EmployeeController() {
super();
DBUtils dbInstance = DBUtils.getInstance("mysql");
System.out.println("DAOFactory successfully obtained: " + dbInstance);
// Obtain UserDAO.
employeeDAO = dbInstance.getEmployeeDAO();
departmentDAO = dbInstance.getDepartmentDAO();
jobDAO = dbInstance.getJobDAO();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
employees = employeeDAO.findAll();
request.setAttribute("employees", employees);
}
my question is how can I map the Department object inside the employeeDAO or its implementation when I call the findAll method of the employeeDAO?
I had something like this in my attempt to map the results :
private Employee map(ResultSet rs) throws SQLException {
Employee employee = new Employee();
employee.setEmployeeID(rs.getInt("EMPLOYEE_ID"));
employee.setFirstName(rs.getString("FIRST_NAME"));
employee.setLastName(rs.getString("LAST_NAME"));
Department department = new DepartmentDAOImpl().getDepartmentByID(rs
.getInt("DEPARTMENT_ID"));
employee.setDepartment(department);
return employee;
}
But I think this is a wrong approach. Can someone help me with this?