Hibernate中sql聯合多個表查詢,並將結果映射成多個實體,再取實體的屬性封裝到一個bean中

       hibernate中能夠用addEntity("o", Order.class)將查詢結果映射成實體,這些實體存放在list中,sql

具體操做以下session

String querysql = "select {o.*},{e.*} from " +sql + " order by o.ORDER_DATE desc "; 
    Query query = session.createSQLQuery(querysql) 
         .addEntity("o", Order.class)
         .addEntity("e", Enterprise.class)
     //一下兩行是分頁 
     query.setFirstResult(page.getStart());
     query.setMaxResults(page.getLimit()); 
     List<HashMap> list = query.list(); 
     List<OrderForBigItem> ofbiList = new ArrayList<OrderForBigItem>();
     //遍歷list 
     Iterator it = list.iterator(); 
     List<Order> orderList = page.getRoot(); 
     List<OrderForEdit> orderFEList = new ArrayList<OrderForEdit>(); 
     while(it.hasNext()){ 
     Object[] result = (Object[])it.next(); 
     Order order = (Order)result[0];
     //獲取list中的bean 
     Enterprise entp = (Enterprise)result[1];
     if(null==entp){
         entp = new Enterprise();
     } 
     String status = getOrderDetailStatus(order);
     //封裝須要使用的屬性到bean中 
     OrderForEdit orderFE = new OrderForEdit(); 
     orderFE.setOrderType(order.getOrderType()); 
}
相關文章
相關標籤/搜索