Hibernate UNION查询返回自定义对象并且分页代码

                StringBuilder sb = new StringBuilder();
		sb.append("select id,from_mine_name,to_mine_name,transit_time,gross_weight,net_weight,check_flag, ");
		sb.append("rate_weight,amount,driver,cardid,card_type,car_type,maker ,coal_type,license_plate,town_name  from( ");
		sb.append("SELECT * , ");
		sb.append("ROW_NUMBER() OVER(ORDER BY transit_time desc) as  ROW_NUM ");
		sb.append(" FROM ( ");
		sb.append(" select  id,from_mine_name,to_mine_name,transit_time,gross_weight,net_weight, check_flag, ");
		sb.append("rate_weight,amount,driver,cardid,card_type,car_type,maker ,coal_type,license_plate,town_name  ");
		sb.append("from you_transfers t  ");
		sb.append("union select id,mine_name,station_name,transit_time,gross_weight,net_weight, '1',");
		sb.append("rate_weight,tax_amount,driver,cardid,card_type,car_type,maker,coal_type,license_plate,town_name ");
		sb.append("from you_measures m ) a  ");
		if (querysb.toString().length() > 0) {
			sb.append(" where  " + querysb.toString());
		}
		sb.append(" )  as d where ROW_NUM BETWEEN (" + page.getPageNumber() + "-1)*" + page.getPageSize() + "+1 AND "
				+ page.getPageNumber() + "*" + page.getPageSize());

		SQLQuery query = getDAO().createSQLQuery(sb.toString()); // sql就是数据库直接可以执行的sql语句;
		// 关键转换方法query.setResultTransformer,参数AliasToBeanResultTransformer(映射到的POJO类).
		query.setResultTransformer(new AliasToBeanResultTransformer(Transfermeasure.class));
		// hibernate会自动映射过去,如果不用这个方法它返回的时候是Collection,所以不能起到转换的作用,在JBoss中运行查询时会报错的.
		List result = query.list(); 
  

你可能感兴趣的:(Java)