i have stats method using JPQL in jpa maven project , i have got that error while testing in the client project.
java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.String
public int StatDoctorBySpeciality()
{
int count = 0 ;
Query query = em.createQuery("SELECT COUNT(u) FROM User AS u where u.role like 'doctor' GROUP BY u.specialite ");
List<Object[]> results = query.getResultList();
for (int i = 0; i < results.size(); i++) {
Object[] arr =(Object[]) results.get(i);
for (int j = 0; j < arr.length; j++) {
System.out.print(arr[j] + " ");
count= (int) arr[j] ;
}
}
return count;
}
arr[j].toString()
– Compass Nov 2 '16 at 15:22+ " "
would work tooSystem.out.print(arr[j]);
, an exception is an object, so thetoString()
ofObject
will be call. PS : I don't see why you cast theget
result since this is the type of your list. – AxelH Nov 2 '16 at 15:25System.out.println()
because you concat it with a String+ " "
. Then you will have an exception because you try to cast intoint
a Class instance – AxelH Nov 2 '16 at 15:28