java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.lang.Integer in DAO

Lilac :

I have the following method in my Dao class

public Collection<Files> findFilesByFolder(Collection<Integer> ids) {
        if (ids.size() > 0) {
            StringBuffer qbe = new StringBuffer("select f from Files f where f.folders.idFolders in ( :id )");
            return super.list(qbe, "id", ids);
        }
        else {
            return new ArrayList<Files>(0);
        }
    }

And when I call this funtion it returns java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.lang.Integer exception

here is the declaration of list().And I hope hibernate is handling the collection. But no idea how this ends up in a class cast exception. Thank you

protected Collection<T> list(StringBuffer qbe, Object... parameterMap) throws InfrastructureException {
        return this.findByQuery(qbe.toString(), parameterMap);
    }

@SuppressWarnings("unchecked")
    public Collection<T> findByQuery(String qbe, Object... parameterMap) throws InfrastructureException {
        return (Collection<T>) anonymousFindByQuery(qbe, parameterMap);
    }
------------------------------------------------------------------------------------------- 
    public Collection<?> anonymousFindByQuery(String qbe, Object ...parameterMap) 
            throws InfrastructureException{
        getSession();
        try {
            Query q = createQuery(qbe, parameterMap);
            return q.list();
        } //end try
        catch (HibernateException ex) {
            WLog.DAOLogger.error("HibernateException", ex);
            throw new InfrastructureException(ex);
        } //end
    }
--------------------------------------------------------------------------------------
protected Query createQuery(String qbe, Object... parameterMap) throws InfrastructureException {
        Session session = getSession();
        Query q = session.createQuery(qbe);

        String formattedQuery = String.format("%s ", qbe);

        for (int i = 0; i < parameterMap.length; i = i + 2) {

            //Put the data in easy to use forms.
            Object key = parameterMap[i];
            Object value = parameterMap[i + 1];
            String formattedKey = String.format(":%s ", key);

            if(value == null || formattedQuery.indexOf(formattedKey) == -1){
                continue;
            }

            if(value instanceof ArrayList){
                q.setParameterList(key.toString(), (Collection<?>)value);
            }
            else{
                q.setParameter(key.toString(), value);
            }
        }
        return q;
    }
Vishal Sharma :

The problem can occur in case you are passing your elements to dao funciton public Collection findFilesByFolder(Collection ids) using Arrays.asList() function.

In that case the object instance will not be of class ArrayList. It will be of type Arrays$ArrayList. I am guessing this based on the error message pasted by you.

Your code flow goes to else part in your loop which calls setParameter instead of setParameterList. When you pass Array$ArrayList object to setParameter function, it tries to cast list object, but fails for obvious reasons, hence the error.

If you pass an ArrayList object which you have created using new ArrayList() function. It should work fine.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=24157&siteId=1