I am using Glassfish & Toplink with MySQL for implementing JPA. The project has a employee entity and a project entity with a many to many relation mapping. (emp_id and project_id are the primary keys respectively)
This is the code in my session bean for fetching Employee along with eager fetching of related projects:
public Employee getEmployeeDetailsById(int id){
Query q = em.createQuery("select e from Employee e join fetch e.projects where e.empId=?1");
q.setParameter(1, id);
Employee e = (Employee) q.getSingleResult();
return e;
}
But when I try to access this method from my client, it gives out an error saying
javax.persistence.NonUniqueResultException: More than one result was returned from Query.getSingleResult()
When I run the query directly on MySQL it returns only a single row, then why this exception is thrown?