I have these two classes (Getter/Setter/Constructor omitted):
public class Company { private String name; private String adress; private List<Employee> employees; private Uuid id; } public class Employee { private String name; private int age; private Uuid id; } The two entities and their relationship are modeled in a relational database using the tables COMPANY, EMPLOYEE and EMPLOYMENT.
Now iI want to write the DAO classes to manage persistance of the two entities and their relationship. The easy way would be making a DAO for each one and have the CompanyDao use the EmployeeDao internally. But according to some Stackoverflow answers iI've read, this is a design smell and should be refactored into service methods which use the DAOs without them having to depend on each other.
This is the way iI would implement this service method:
public class DBService { public DBService(CompanyDao companyDao, EmployeeDao employeeDao, EmploymentDao employmentDao) { ... } public Company findCompany(Uuid companyId) { Company company = companyDao.find(companyId); List<Uuid> employeeIds = employmentDao.findEmployeeIds(company.getId()); for(Uuid employeeId : employeeIds) { company.addEmployee(employeeDao.find(employeeId)); } } } Is this a good way to do this? Or should the EmployeeDao have a findByCompany(Uuid companyId) method?
Info: I already asked a smiliar question on Stackoverflow, but the only answers iI got was "Use an ORM tool". I know that something like Hibernate would manage all of the persistance for me, but I would like to know how to do this by hand.