hql 多表连接查询

来源:互联网 发布:淘宝卖家的快递费 编辑:程序博客网 时间:2024/05/02 00:34
 到网了找了N久的资料,没看到一个满意的答案``还是自己发点时间研究了一下。

原码如下:

/**
     * 统计
     *
     * @return
     * @throws BusinessExceptions
     */
    public List statistic(Integer type, String productId, String clientId,
            Date dateBirthday1, Date dateBirthday2, String storeId,
            Integer price, String employeeId) throws BusinessExceptions {
        StringBuffer sb = new StringBuffer("select new map(");
        sb.append("osbi.id as id ,");
        sb.append("osbi.sku.product.name as productName,");
        sb.append("osbi.sku.color.name as colorName,");
        sb.append("osbi.sku.size.name as sizeName,");
        sb.append("osbi.outStoreBill.employee.name as employeeName,");
        sb.append("osbi.outStoreBill.client.name as clientName,");
        sb.append("osbi.outStoreBill.store.name as storeName,");
        sb.append("sum(osbi.price) as totalPrice,");
        sb.append("sum(osbi.quantity) as totalQuantity ");
        sb.append(") from OutStoreBillItem as osbi ");
        sb.append("left join osbi.sku left join osbi.outStoreBill ");
        sb.append("left join osbi.sku.product ");
        sb.append("left join osbi.sku.color ");
        sb.append("left join osbi.sku.size ");
        sb.append("left join osbi.outStoreBill.store ");
        sb.append("left join osbi.outStoreBill.client ");
        sb.append("left join osbi.outStoreBill.employee ");
        sb.append(" where osbi.outStoreBill.type = ?");
        sb.append(" and osbi.sku.product.id = ?");
        // sb.append(" and osbi.outStoreBill.state = ?");
        // sb.append(" and osbi.outStoreBill.client.id = ?");
        sb.append(" and osbi.outStoreBill.createTime between ? and ?");
        sb.append(" and osbi.outStoreBill.store.id = ?");
        // sb.append(" and osbi.outStoreBill.employee.id = ?");
        sb.append(" group by osbi.sku.id");
        return this.find(sb.toString(), type, productId, dateBirthday1,
                dateBirthday2, storeId);
    }

OutStoreBillItem 对像中有一个outStoreBill的属性,OutStoreBill对像中又有store,client,employee等属性。
OutStoreBillItem 对像中有一个sku的属性,Sku对像中又有product等属性

上例统计OutStoreBillItem表。left join了 outStoreBill表。而outStoreBill又left join了store,client,employee三表。