​ 最近碰到了BUG,在操作hibernate的SQL结果集的时候报类型转换错误,在这个问题上花费了好多时间,就将这个BUG的解决心得写在了这篇博客里面。

取出hibernate执行的SQL的结果集

java取出数据报错

最近在一个SSH项目中碰到了一个获取数据的问题,SQL执行后的结果集使用类型转换提取数据时报错。

  1. 下面是DAO层实现类中的方法:返回了结果集List
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public List<TblSwitch> getPagelist(Integer limit, Integer offser, String where, Object[] obj) {
logger.info("get getPagelist by:");
String sql="select a.add_user, "
+ " a.old_protocol,a.now_protocol,a.mobile,a.add_time,a.delete_mark,a.update_time,a.id,a.remark "
+ " from tbl_switch a where 1=1 and a.delete_mark=0 "+where+" ORDER BY a.add_time DESC ";
List<TblSwitch> list=null;
try{
Query query=this.getSession().createSQLQuery(sql).setFirstResult(limit).setMaxResults(offser);
if(null !=obj&&obj.length>0){
for(int i=0;i<obj.length;i++){
query.setParameter(i, obj[i]);
}
}
list=query.list();
}catch(Exception e){
logger.error("get page getPagelist error:"+e.getMessage());
e.printStackTrace();
}
return list;
}
  1. 在控制层对结果集list进行操作

​ 当我想对结果集进行操作的时候,发现无法对类型进行强制转换(将list中的每个对象强转为TblSwitch类型),在这条语句中会报错,但是我需要取出每个对象的内容。

1
2
3
for (int i = 0; i < list.size(); i++) {
TblSwitch tblSwitch1=list.get(i);
}

​ 强转Map同样报错

1
Map<String, Object> dataMap = (Map<String, Object>) list.get(i);

​ 强转Map报错信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
27-Sep-2019 15:47:26.547 涓ラ噸 [http-nio-8080-exec-8] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [msg] in context with path [] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.util.Map] with root cause
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.util.Map
at com.zhirong.base.web.SwitchNumberController.popSumExport(SwitchNumberController.java:547)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:203)
at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:181)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

解决方法:

方法一:

将对象赋值给Object map,这时map中有list中的对象的数据,但是不能用map.getAddUser()的方法获取,这时我将map强制转换成了Object[] objs,这样就将一个对象的属性用数组的形式保存起来了,通过数组下标取值,获取数据。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
for (int i = 0; i < list.size(); i++) {
Object map=list.get(i);
Object[] objs=(Object[])map; //强转object数组对象
if (null!=objs[0]){
adduser=objs[0].toString();
}
if (null!=objs[1]){
oldProtocol=objs[1].toString();
}
if (null!=objs[2]){
nowProtocol=objs[2].toString();
}
if (null!=objs[3]){
mobile=objs[3].toString();
}
if (null!=objs[4]){
addtime=objs[4].toString();
}
}

方法二:

将SQL结果返回值设置为List,不在是List<TblSwitch>,这时在遍历list的时候可以将list中的对象强转为Map类型。

  1. 修改返回值:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public List getPagelist(Integer limit, Integer offser,String where, Object[] obj) {
logger.info("get getPagelist by:");
String sql="select a.add_user, "
+ " a.old_protocol,a.now_protocol,a.mobile,a.add_time,a.delete_mark,a.update_time,a.id,a.remark "
+ " from tbl_switch a where 1=1 and a.delete_mark=0 "+where+" ORDER BY a.add_time DESC ";
List list=null;
try{
Query query=this.getSession().createSQLQuery(sql).setFirstResult(limit).setMaxResults(offser);
if(null !=obj&&obj.length>0){
for(int i=0;i<obj.length;i++){
query.setParameter(i, obj[i]);
}
}
list=query.list();
}catch(Exception e){
logger.error("get page getPagelist error:"+e.getMessage());
e.printStackTrace();
}
return list;
}
  1. 使用Map接收list的对象
1
Map<String, Object> dataMap = (Map<String, Object>) list.get(i);

页面取值

页面取值也是同样使用下标获取数据。

  1. c:if 判断list是否为空;
1
<c:if test="${null != list}">
  1. c:forEach 遍历list;
1
<c:forEach items="${list}" var="dataList" varStatus="status">
  1. ${} 取值,利用下标确定对象中的某个属性。
1
${dataList[3]}
  1. 页面部分代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
	<c:if test="${null != list}">
<c:forEach items="${list}" var="dataList" varStatus="status">
<c:choose>
<c:when test="${status.count % 2 == 0}">
<tr style="background-color:#FFFFFF" onclick="Zving.DataGrid.onRowClick(this,event);">
</c:when>
<c:otherwise>
<tr style="background-color:#F9FBFC" onclick="Zving.DataGrid.onRowClick(this,event);">
</c:otherwise>
</c:choose>
<td rowno="${(page.page*page.rows) + (status.index+1) }" class="rowNo" style="text-align: center;">
${(status.index+1)+(page.page*page.rows)}
</td>
<td title="${dataList[3] }" style="text-align: center;">
${dataList[3]}
</td>
<td title="${dataList[1] }" style="text-align: center;">
<c:if test="${dataList[1]=='0'}">移动</c:if>
<c:if test="${dataList[1]=='1'}">联通</c:if>
<c:if test="${dataList[1]=='2'}">电信</c:if>
</td>
<td title="${dataList[2] }" style="text-align: center;">
<c:if test="${dataList[2]=='0'}">移动</c:if>
<c:if test="${dataList[2]=='1'}">联通</c:if>
<c:if test="${dataList[2]=='2'}">电信</c:if>
</td>
<td title="<fmt:formatDate value="${dataList[4]}" pattern="yyyy-MM-dd" />" style="text-align: center;">
<fmt:formatDate value="${dataList[4]}" pattern="yyyy-MM-dd" />
</td>
<td title="${dataList[0]}" style="text-align: center;">${dataList[0]}</td>
<td title="" style="text-align: center;">
&nbsp;&nbsp;
<a href="#" onclick="return doUpdate(${dataList[7]})">修改</a>
&nbsp;&nbsp;
<a href="#" onclick="return del(${dataList[7]})">删除</a>
</td>
</tr>
</c:forEach>
</c:if>

最后更新: 2020年06月11日 10:14

原始链接: http://ligangit.com/2019/09/25/取出hibernate执行的SQL的结果集/

× 请我吃糖~
打赏二维码