java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.util.Map的问题
代码如下:publicList<Map<String,Object>>queryStu()throwsException{return(List<Map<String,O...
代码如下:
public List<Map<String, Object>> queryStu() throws Exception {
return (List<Map<String, Object>>)this.getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
System.out.println("daoimpl..doinhibetrnate..");
String hql = "select count(distinct sid) from Student";
Query q = session.createQuery(hql);
Object o = q.uniqueResult();//总记录数
allPage = (Integer.valueOf(o.toString()) + pageSize - 1)/pageSize;//总页数
allog = Integer.valueOf(o.toString());
String hql2 = "select sid,sname,ssex,sage,sdept,sclass from Student";
Query q2 = session.createQuery(hql2);
q2.setFirstResult((currentPage - 1)*pageSize);//起始数据
q2.setMaxResults(pageSize);//每页的条数
List<Map<String, Object>> list = q2.list();//object类型,转为map报错
System.out.println("daompl.....+"+list);
for(Map<String, Object> map:list){//object强转map
System.out.println("list-------");
for(String s:map.keySet()){
System.out.println(s+":"+map.keySet());
}
}
return list;
}
});
}
在下面的for循环输出有错,怎么回事? 展开
public List<Map<String, Object>> queryStu() throws Exception {
return (List<Map<String, Object>>)this.getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
System.out.println("daoimpl..doinhibetrnate..");
String hql = "select count(distinct sid) from Student";
Query q = session.createQuery(hql);
Object o = q.uniqueResult();//总记录数
allPage = (Integer.valueOf(o.toString()) + pageSize - 1)/pageSize;//总页数
allog = Integer.valueOf(o.toString());
String hql2 = "select sid,sname,ssex,sage,sdept,sclass from Student";
Query q2 = session.createQuery(hql2);
q2.setFirstResult((currentPage - 1)*pageSize);//起始数据
q2.setMaxResults(pageSize);//每页的条数
List<Map<String, Object>> list = q2.list();//object类型,转为map报错
System.out.println("daompl.....+"+list);
for(Map<String, Object> map:list){//object强转map
System.out.println("list-------");
for(String s:map.keySet()){
System.out.println(s+":"+map.keySet());
}
}
return list;
}
});
}
在下面的for循环输出有错,怎么回事? 展开
展开全部
List<Map<String, Object>> list = q2.list();// object类型,转为map报错这里应该写成:
List<Object>list =q2.list();
for(Object object:list)
{
Object[] obj =(Object[])object;
//然后进行输入obj数组里面的值即可
System.out.println(obj[0]);
System.out.println(obj[1]);
//....
}
List<Object>list =q2.list();
for(Object object:list)
{
Object[] obj =(Object[])object;
//然后进行输入obj数组里面的值即可
System.out.println(obj[0]);
System.out.println(obj[1]);
//....
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Hibernate 分页查询的方法list 返回的是List<实体类>吧 ,你什么返回List<Map>,看你hql应该是List<Student>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询