HQL复合查询例子

	//原生sql与HQL的区别
		/*
		 * 不能直接使用表名,用类名,首字母大写
		 * 关联关系: 多的一方的“属性”(字段)等于一的一方的“表”(类)别名:多:goods gs,orders o一:,goodstype gt,orderdetails:ol
		 */
		String hql="select new Map(gt.name as name,sum(ol.money) as y) "+
						"from Goodstype gt,Goods gs,Orderdetail ol,Orders o "+
						"where ol.goodsuuid = gs.uuid and gs.goodstype = gt "+
						"and ol.orders = o and o.type='2' "+ 
						"group by gt.name ";
		return getHibernateTemplate().find(hql);

调用的结果:转成了json格式

原生的SqL语句:

select gt.name,sum(ol.money)
            from goodstype gt,goods gs,orderdetail ol,orders o
            where ol.goodsuuid = gs.uuid and gs.goodstypeuuid = gt.uuid 
            and o.uuid=ol.ordersuuid and o.type='2'
            group by gt.name 

表名转成类名:eg:goodstype-->Goodstype

表字段名转为类属性名:eg:gs.goodstypeuuid-->gs.goodstype 

表之间的关联关系,多的一方保留属性名,一的一方写类/类的别名,eg:gs.goodstype=gt.uuid--->gs.goodstype=gt(gt一的一方的类别名,gs是Goods的别名,是多的一方,一种商品类型Goodstype对应多种商品Goods)

猜你喜欢

转载自blog.csdn.net/cl723401/article/details/87937414