I am iterating the objects obtained from a Native SQL query to be able to print the propierties of one entity. For this propose I am using the following code:
public<T> ArrayList exectuteStoreProcedure(String sqlQuery){
setEntities();
int colcount = 0;
int rowcount = 0;
int rowcounter = 0;
ArrayList a = new ArrayList();
try{
Query query = em.createNativeQuery(sqlQuery);
rowcount = query.getResultList().size();
colcount = ((Class[]) query.getResultList().get(0)).length;
String [][] array = new String [rowcount][colcount];
for (Iterator<String[]> rs = query.getResultList().iterator(); rs.hasNext();) {
String[] obj = rs.next();
String[] record = new String[colcount];
System.arraycopy(obj, 0, record, 0, colcount);
array[ rowcounter++] = (String[]) record;
}
a.add(array);
}catch(Exception ex){
et.rollback();
}
closeEntities();
return a;
}
The execution fails throwing the following exception:
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Class;
After running the programme over debbug mode I found that the problem is with the declartion of (Class[]) on this command line:
colcount = ((Class[]) query.getResultList().get(0)).length;
I am wondering how I could get the column length. Does anyone know how? My entity is Idioma and it's composed by String palabra, String idioma and int wordID;
Thanks in advance!