日期:2014-05-17  浏览次数:20786 次

请教一个java中将查询结果中的Object转换为list的问题
我在项目开发中通过hibernate的getQuery()方法获得查询结果,并传给一个list代码如下:
  _RootDAO.initialize();
Session s = doi15DAO.getSession();
System.out.println(sql);
Query q = doi15DAO.getQuery(sql, s);

  List <DimOptInt15min> DOI_List = new ArrayList<DimOptInt15min>();
List list = q.list();
得到一个list,我想做的是把得到的list强制转换为DimOptInt15min的类型。但遇到java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to DimOptInt15min的错误。然后我想把这个list的值一项一项取出来,然后转换为DimOptInt15min类型,再放入一个List <DimOptInt15min> DOI_List = new ArrayList<DimOptInt15min>()中,代码如下:
DimOptInt15min doi15 = new DimOptInt15min();
DimOptInt15minPK dopi_pk = new DimOptInt15minPK();// 该表的主键
dopi_pk.setIntersectionid(Integer.parseInt(((List)q.list().get(i)).get(0).toString()));
此行报错 dopi_pk.setCalendarKey(Integer.parseInt(((List)q.list().get(i)).get(1).toString()));
dopi_pk.setFifteenx(Integer.parseInt(((List)q.list().get(i)).get(2).toString()));
doi15.setId(dopi_pk);
doi15.setEbVolumeL(Integer.parseInt(((List)q.list().get(i)).get(3).toString()));
doi15.setEbVolumeS(Integer.parseInt(((List)q.list().get(i)).get(6).toString()));
doi15.setEbVolumeR(Integer.parseInt(((List)q.list().get(i)).get(9).toString()));

再次出现的错误为:严重: Servlet.service() for servlet Post_test_demo threw exception
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.util.List
at org.openflashchart.chartdemo.Post_intersection_data.select(Post_intersection_data.java:172)
at org.openflashchart.chartdemo.Post_intersection_data.doPost(Post_intersection_data.java:148)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)

难道不可以将Object转换为List吗??困扰我一天了,求高手指点一下,万分感谢。


------解决方案--------------------
1)List list = q.list(); //应该是个对象list,LZ可以这样试试
List list = q.list();
DimOptInt15min[] DOI_List = (DimOptInt15min[] )list.toArray(new DimOptInt15min[list.size()]);

2)第二种方法
List list = q.list();
List <DimOptInt15min> DOI_List = new ArrayList<DimOptInt15min>();
for( Object item : lst ) {
DOI_List.add((DimOptInt15min)item);
}
------解决方案--------------------
也不是说方法一不对,是不一定对,因为不知道LZ的Object数组里保存的是什么