reset password
Author Message
Vanquish39
Posts: 134
Posted 13:13 Oct 16, 2011 |

If any of you are having problems returning a list of objects from the getResultList(), this explains a difference between 2 different implementations.

Let's say you have a Patient class and inside this patient class you have a list of insurance objects.

 

        //Querying way #1.  If you don't specify select in the query, you will get back all the objects.
       

        Query query = manager.createQuery("from Patient p join p.insurances i where i.name = :name");
        query.setParameter("name", "Medicaid");
        List<Object[]> patients = (List<Object[]>)query.getResultList();
       
        for(Object[] p: patients)
        {
            Patient patient = (Patient)p[0];
            Insurance ins = (Insurance)p[1];
           
            System.out.println(patient.getName() + ", ins: " + ins.getName());
        }
       
        //Querying way #2.  When you have the select inside the query, you will get only the object you are selecting.
       

       Query qq = manager.createQuery("select p from Patient p join p.insurances i where i.name = :name");
        qq.setParameter("name", "Medicaid");
        List<Patient> patients1 = (List<Patient>)qq.getResultList();
       
        for(Patient p: patients1){
            System.out.println("Patient Name: " + p.getName());
           
            List<Insurance> ins = p.getInsurances();
           
            for(Insurance x: ins)
                System.out.println(x.getName());
        }

 

If anyone was having this problem, this is probably your solution.