获取多个实体关联查询,封闭到对象
一、Hibernate Query查询部分属性
select new com.golf.back.dto.video.VideFavourDetailDto(v.id, v.thumb, v.videoName, v.videoUrl, v.orgUrl, v.orgUrlSmall, vc.catName, vc.parentId, v.summary, v.content, v.price,v.isFree, v.lookFront, vf.createTime) from VideoFavour vf,Video v ,VideoCat vc where vf.videoId=v.id and v.catId=vc.id
VideFavourDetailDto类添加相同参数类型及个数的实例化对象;
二、多表查询,返回多个实体
StringBuffer sql = new StringBuffer("select {vpl.*}, {v.*}, {vc.*} from zg_video_playlog vpl "); sql.append("left join zg_video v on vpl.video_id=v.id "); sql.append("left join zg_video_cat vc on vc.id=v.cat_id "); sql.append("group by vpl.video_id "); sql.append("order by vpl.id desc"); Query query = super.getSession().createSQLQuery(sql.toString()) .addEntity( "vpl" , VideoPlayLog.class ) .addEntity( "v" , Video.class ) .addEntity("vc", VideoCat.class); query.setFirstResult(startPos); query.setMaxResults(pageSize); List<Object[]> list = query.list(); List<VideFavourDetailDto> vdList = new ArrayList<VideFavourDetailDto>(); if(null!=list && list.size()>0){ VideFavourDetailDto dto = null; VideoPlayLog vpl = null; Video v = null; VideoCat vc = null; for(int i=0; i<list.size(); i++){ vpl = (VideoPlayLog)list.get(i)[0]; v = (Video)list.get(i)[1]; vc = (VideoCat)list.get(i)[2]; dto = new VideFavourDetailDto(vpl.getId(), v.getId(), v.getThumb(), v.getVideoName(), v.getVideoUrl(), v.getOrgUrl() , v.getOrgUrlSmall(), vc.getCatName(), vc.getParentId(), v.getSummary(), v.getContent(), v.getPrice(), v.getIsFree(), v.getLookFront(), vpl.getCreateTime(), vpl.getPlayTimes()); vdList.add(dto); } }
特别注意: 这块有些问题,明明应该返回Clazz的List的,但确返回包含Clazz和Student的Object的List, 使用场合:基于对象的查询,复杂查询