0

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!

1 Answer 1

1

The error say it all, that is not an array of Class, it's as array of Object. You can change your cast and use:

colcount = ((Object[]) query.getResultList().get(0)).length;  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.