欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

hibernate多表查询出结果转化为实体类

程序员文章站 2022-04-14 09:17:23
...

hibernate多表查询出结果后得到一个object对象,想用对象调用时会报object无法转化错误

所以要将object转化为对象

不知道有没有更好的方法,知道的请告知

public List<ArticleContent> listToVO(List<Object> arlist){
		List<ArticleContent> list = new LinkedList<ArticleContent>();
		if(arlist != null && arlist.size() > 0){
			ArticleContent articleContent;
			for(int i = 0; i < arlist.size();i++){
				articleContent = new ArticleContent();
			     Object[] object = (Object[])arlist.get(i);
			     String idkey =  (String)object[0];
			     String articleName =  (String)object[1];
			     String isEnable =  (String)object[2];
			     String content =  (String)object[3];
			     String createDate =  (String)object[4];
			     String createName =  (String)object[5];
			     articleContent.setIdkey(idkey);
			     articleContent.setArticleName(articleName);
			     articleContent.setContent(content);
			     articleContent.setIsEnable(isEnable);
			     articleContent.setCreateDate(createDate);
			     articleContent.setCreateName(createName);
			     list.add(articleContent);
			}
		}
		return list;
	}

这是我封装的一个方法,首先遍历list查询结果,按下标取值

@SuppressWarnings("unchecked")
	public List<ArticleContent> selectArticleContentList(){
		String hql = "select a.idkey,a.articleName,a.isEnable,a.content,a.createDate,a.createName,"
				+ "(select m.menuName from MenuManage m where a.menuCode = m.menuCode)as menuName from ArticleContent a order by a.createDate DESC";
		List<Object> arlist = this.getSuperDao().find(hql);
		List<ArticleContent> list = new LinkedList<ArticleContent>();
		if(arlist != null && arlist.size() > 0){
			ArticleContent articleContent;
			for(int i = 0; i < arlist.size();i++){
				articleContent = new ArticleContent();
			     Object[] object = (Object[])arlist.get(i);
			     String idkey =  (String)object[0];
			     String articleName =  (String)object[1];
			     String isEnable =  (String)object[2];
			     String content =  (String)object[3];
			     String createDate =  (String)object[4];
			     String createName =  (String)object[5];
			     String menuName =  (String)object[6];
			     articleContent.setIdkey(idkey);
			     articleContent.setArticleName(articleName);
			     articleContent.setContent(content);
			     articleContent.setIsEnable(isEnable);
			     articleContent.setCreateDate(createDate);
			     articleContent.setCreateName(createName);
			     articleContent.setMenuName(menuName);
			     list.add(articleContent);
			}
		}
		return list;
	}