• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JPA - getSingleResult Error

 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic