I’m doing a query which returns objects in stead of a custom class:
Query q = em.createQuery("FROM Author a LEFT JOIN a.documents d LEFT JOIN d.personDocuments pd WHERE pd.person = :person ORDER BY a.lastName");
q.setParameter("person", person);
return new HashSet<Author>(q.getResultList());
I would expect that q.getResultList() would return a list of Authors (and their linked documents), however it’s a list of objects. What am I doing wrong?
The method
getResultList()from Query is not typed.Therefore it seems that it only returns
Objects(which really are instances ofAuthorin your case), which you manually have to cast toAuthor. (You can check what theObjectreally is by inspectinggetClass()on an instance for example.)You can use TypedQuery for typed queries, which will work as you need them.
This Tutorial from objectdb covers it quite well.