Rob Hills wrote: > Hi Trish, > > Trish wrote: >> Hi >> >> Im new to appfuse and trying to implement manay to many relation for >> Employee Project tables, the created model classes as follows >> > At a quick glance, your entities look OK to me. >> public List<Employee> getByProject(Long projectId, Long periodId) { >> return getHibernateTemplate().find("from Employee where employeeId in " + >> "(select employeeId from EmployeesProject " + >> "where projectId=?"+ projectId+")"); >> }
It's time for me to give back to the group! You're trying to use SQL syntax, in an HQL query. It's been a frustration for me too - it would have been easier if HQL had made a clean break from SQL. What you want to do here is find all the Employees for a Project, so the query should be something more like: public List<Employee> getByProject(Long projectId, Long periodId) { return getHibernateTemplate().find( "select employee from EmployeesProject " + "where project.projectId=?)" ,projectId); } There are a couple of other issues - I assume the fact that you have two getters for Employee in EmployeesProject was just an accident, as it wouldn't compile! Your Employee and Project classes define the employee-project relationship as OneToMany in both directions. I suppose that can work as you've done it, but if you just define the relationship as ManyToMany you don't even need to explicitly define the EmployeesProject class, it will be done automagically. Note that you only specify the "mapped by" in one direction (it probably makes some difference in efficiency which side of the relationship you use, but no difference in your programming). I have to admit, I don't know whether you could use the HQL above if the join class is implicit - you might need "select employees from Project where projectId=?". @Entity public class Employee extends BaseObject { private Long employeeId; private String employeeName; private List<Project> projects; ... @ManyToMany(mappedBy = "projects") public List<Project> getProjects() { return projects; } ...... } @Entity public class Project extends BaseObject { private Long projectId; private String projectName; private List<Employee> employees; ... @ManyToMany public List<Employee> getEmployees() { return employees; } ......... } -- derek --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]