This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA - getSingleResult Error

 
nitin pai
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is because you are doing a LEFT JOIN FETCH in you query.

Try doing this instead:


You should get a sigle result then.
 
nitin pai
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergio, I actually want to do a JOIN FETCH query since my Employee entity has a collection of Project entities and I want them to be eagerly loaded when my Employee entity is fetched. But the query I mentioned above is giving the error. The query which you have given works fine but Project collection is not fetched.
Can you explain why is this not the case when I use JOIN FETCH?
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because it is doing a join (an outer join in fact). If you have more than one project related to that employee, you'll get more than one result.

This is the SQL equivalent of what you are doing:
 
tiago carneiro
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your query


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;
}


you have to use DISTINCT, like this:




see the spec:

A fetch join has the same join semantics as the corresponding inner or outer join, except that the related objects specified on the right-hand side of the join operation are not returned in the query result or otherwise referenced in the query. Hence, for example, if magazine id 1 has five articles, the above query returns five references to the magazine 1 entity.

Regards,
Tiago
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic