我无法从对象变量类型转换为类类型变量。出现以下错误:
*Hibernate: select osinfo0_.host_id as host_id1_17_0_, osinfo0_.os_build as os_build2_17_0_, osinfo0_.os_name as os_name3_17_0_, osinfo0_.os_type as os_type4_17_0_, osinfo0_.os_version as os_versi5_17_0_ from hwi.os_info osinfo0_ where osinfo0_.host_id=?
Jun 15, 2015 10:28:20 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [spring] in context with path [/HardwareInventory] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.vmware.hwi.model.StorageInfo] with root cause
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.vmware.hwi.model.StorageInfo
at com.vmware.hwi.dao.impl.StorageInfoDaoImpl.getStorageInfoFullListByTeam(StorageInfoDaoImpl.java:76)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)*
我是Hibernate Query编写的新手,所以我怀疑编写查询时可能会有一些错误,但我可以看到查询返回的值完全正常,不知道我在这里缺少的地方。
以下是代码的一部分:
@Override
public StorageInfo getStorageInfoFullListByTeam(Integer storageTeam){
StorageInfo storageFullList = null;
Session session = sessionFactory.getCurrentSession();
/*Query query = session.createQuery(
"from Host host, StorageInfo storage ServerStorage" +
" server where storage.storageId=server.storageId" +
" AND server.hostId=host.hostId AND storage.storageTeam= ' "
+ storageTeam+ " ' ");*/
Query query = session.createQuery(
"from Host host, StorageInfo storage , ServerStorage" +
" server where storage.storageId=server.storageId" +
" AND server.hostId=host.hostId AND storage.storageTeam= ' "
+ storageTeam + " ' ");
List info = query.list();
if(info.size() !=0){
storageFullList = (StorageInfo) info.get(0);
}
return storageFullList;
}
答案 0 :(得分:2)
从您的查询中看起来您正在获取Host,StorageInfo,ServerStorage,并且在获取Object(StorageInfo)info.get(0)时,您尝试将其转换为StorageInfo。 因此,尝试从查询中删除Host和Serverstorage,否则如果要从3个表中获取记录,请创建一个java pojo类,它表示您在查询中获取的列类型并映射您的pojo类(尝试搜索映射pojo到hibernate查询结果)。