解决java.util.LinkedHashMap cannot be cast to xxx

查询返回为List courseList2,但实际courseList2存放的是HashMap,所以在遍历courseList2时报java.util.LinkedHashMap cannot be cast to xxx错误,
参考:https://blog.csdn.net/qq_21104515/article/details/80915183
实体类

public class Course{
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

强制转换没有报错List allList = (List)this.findHql(courseQuery.getHqlNoGroupBy());循环时报错

for(Object obj:courseList2) {
}

解决方法:导入 net.sf.json 类,使用JSONObject中的方法, 先将数据转成json字符串, 在转成实体对象

for(Object obj:courseList2) {
	JSONObject jsonObject=JSONObject.fromObject(obj);
	CourseDto c2 = (CourseDto)JSONObject.toBean(jsonObject, CourseDto.class);
}

你可能感兴趣的:(解决java.util.LinkedHashMap cannot be cast to xxx)