java.lang.ClassCastException: java.util.Arrays$ArrayList 无法转换为 DAO 中的 java.lang.Integer
java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.lang.Integer in DAO
我的 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);
}
}
当我调用这个函数时 returns java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.lang.Integer exception
这是 list() 的声明。我希望 hibernate 正在处理该集合。但是不知道这如何以 class 转换异常结束。谢谢
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;
}
如果您使用 Arrays.asList() 函数将元素传递给 dao 函数 public Collection findFilesByFolder(Collection ids),就会出现此问题。
在那种情况下,对象实例将不是 class ArrayList。它将是 Arrays$ArrayList 类型。我是根据您粘贴的错误消息猜测的。
您的代码流转到循环中的其他部分,该部分调用 setParameter 而不是 setParameterList。当您将 Array$ArrayList 对象传递给 setParameter 函数时,它会尝试转换列表对象,但由于显而易见的原因而失败,因此出现错误。
如果您传递使用新的 ArrayList() 函数创建的 ArrayList 对象。它应该可以正常工作。
我的 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);
}
}
当我调用这个函数时 returns java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.lang.Integer exception
这是 list() 的声明。我希望 hibernate 正在处理该集合。但是不知道这如何以 class 转换异常结束。谢谢
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;
}
如果您使用 Arrays.asList() 函数将元素传递给 dao 函数 public Collection findFilesByFolder(Collection ids),就会出现此问题。
在那种情况下,对象实例将不是 class ArrayList。它将是 Arrays$ArrayList 类型。我是根据您粘贴的错误消息猜测的。
您的代码流转到循环中的其他部分,该部分调用 setParameter 而不是 setParameterList。当您将 Array$ArrayList 对象传递给 setParameter 函数时,它会尝试转换列表对象,但由于显而易见的原因而失败,因此出现错误。
如果您传递使用新的 ArrayList() 函数创建的 ArrayList 对象。它应该可以正常工作。