这有三张表,对应三个实体类表:chanage_cabinets(`ID` int,`APPLICATION_ID` int,`TYPE` varchar)
实体类:ChanageCabinets(Integer id,Integer applicationId,String type)表:chanage_apply(`ID` int,`OPERATION_ID` int,`PRODUCT_ID` int,`MRL` int)
实体类:ChanageApply(Integer id,Integer operationId,Integer productId,Integer mrl)表:chanage_cabinets_execute(`ID` int,,`APPLICATION_ID` int,`ATTITUDE` varchar)
实体类:ChanageCabinetsExecute(Integer id,Integer applicationId,String attitude)关联关系:chanage_cabinets和chanage_cabinets_execute的APPLICATION_ID都引用自chanage_apply的id
附上本地sql查询的语句select c.MRL,
c.OPERATION_ID,
c.PRODUCT_ID,
sum(if(d.TYPE='ONSHOW',1,0)) as onshowCount,
sum(if(d.TYPE='WITHDRAW',1,0)) as withdrawCount,
sum(if(d.TYPE='MOVE',1,0)) as moveCount,
sum(if(d.TYPE='WITHDRAW' and d.REVERSION='RETURN',1,0)) as reversionCount
from chanage_apply c,
chanage_cabinets d,
chanage_cabinets_execute e
where c.ID=d.APPLICATION_ID
and c.ID=e.APPLICATION_ID
and e.attitude='Y'
group by c.MRL,c.OPERATION_ID,c.PRODUCT_ID;
相信大家都能看懂以上语句
要求按chanage_apply表的MRL,OPERATION_ID,PRODUCT_ID依次分组,
并统计分组后chanage_cabinets表里面TYPE为'ONSHOW'时的数量,'WITHDRAW'时的数量,
'MOVE'时的数量,TYPE为'WITHDRAW'并且REVERSION为'RETURN'时的数量。也就是4个统计数值要用hibernate hql写,那些条件以及分组都好写,就是那4个统计数值不知道怎么写,
应该是用嵌套的hql语句
大家都来出出点子
实体类:ChanageCabinets(Integer id,Integer applicationId,String type)表:chanage_apply(`ID` int,`OPERATION_ID` int,`PRODUCT_ID` int,`MRL` int)
实体类:ChanageApply(Integer id,Integer operationId,Integer productId,Integer mrl)表:chanage_cabinets_execute(`ID` int,,`APPLICATION_ID` int,`ATTITUDE` varchar)
实体类:ChanageCabinetsExecute(Integer id,Integer applicationId,String attitude)关联关系:chanage_cabinets和chanage_cabinets_execute的APPLICATION_ID都引用自chanage_apply的id
附上本地sql查询的语句select c.MRL,
c.OPERATION_ID,
c.PRODUCT_ID,
sum(if(d.TYPE='ONSHOW',1,0)) as onshowCount,
sum(if(d.TYPE='WITHDRAW',1,0)) as withdrawCount,
sum(if(d.TYPE='MOVE',1,0)) as moveCount,
sum(if(d.TYPE='WITHDRAW' and d.REVERSION='RETURN',1,0)) as reversionCount
from chanage_apply c,
chanage_cabinets d,
chanage_cabinets_execute e
where c.ID=d.APPLICATION_ID
and c.ID=e.APPLICATION_ID
and e.attitude='Y'
group by c.MRL,c.OPERATION_ID,c.PRODUCT_ID;
相信大家都能看懂以上语句
要求按chanage_apply表的MRL,OPERATION_ID,PRODUCT_ID依次分组,
并统计分组后chanage_cabinets表里面TYPE为'ONSHOW'时的数量,'WITHDRAW'时的数量,
'MOVE'时的数量,TYPE为'WITHDRAW'并且REVERSION为'RETURN'时的数量。也就是4个统计数值要用hibernate hql写,那些条件以及分组都好写,就是那4个统计数值不知道怎么写,
应该是用嵌套的hql语句
大家都来出出点子
解决方案 »
- hibernate中一级缓存和二级缓存的具体区别是什么?
- onclick传参问题
- 菜鸟求助,请各位高手帮帮忙,谢谢
- 我在Java的后台中设置的cookie,在页面如何使用JavaScript得到?
- 如何解决DOM4J Element.add 后产生的XML带有xmlns = ""
- 【急】请教一个 ERROR [STDERR] java.lang.NumberFormatException: For input string: "" 的问题,如能解答奉上所有积分!!
- 请问如何下载jsp文件
- 多表联合上下级查询的问题
- POI按模板生成多个sheet的问题
- struts2自动类型转换问题
- java如何接入银行
- 一个servlet中的问题
直接用SQL语句HIBERNATE两者都支持
让系统与具体用什么数据库不要绑定在一起。
用hibernate不就是做到了与数据库低耦合吗?项目里除了hql查询就是DetachedCriteria查询,不用sql
只是现在我做的资产统计管理中一些统计查询语句不好写,我才先使用sql来实现功能,让页面先展现出来。让领导先看到我做出来了什么东西,
再去完善代码
一个返回聚集值(aggregate values)的查询可以按照一个返回的类或组件(components)中的任何属性(property)进行分组: select cat.color, sum(cat.weight), count(cat)
from Cat cat
group by cat.color
select foo.id, avg(name), max(name)
from Foo foo join foo.names name
group by foo.id
having子句在这里也允许使用. select cat.color, sum(cat.weight), count(cat)
from Cat cat
group by cat.color
having cat.color in (eg.Color.TABBY, eg.Color.BLACK)如果底层的数据库支持的话(例如不能在MySQL中使用),SQL的一般函数与聚集函数也可以出现 在having与order by 子句中。 select cat
from Cat cat
join cat.kittens kitten
group by cat
having avg(kitten.weight) > 100
order by count(kitten) asc, sum(kitten.weight) desc注意group by子句与 order by子句中都不能包含算术表达式(arithmetic expressions).
chanage_cabinets d,
chanage_cabinets_execute e
where c.ID=d.APPLICATION_ID
and c.ID=e.APPLICATION_ID
and e.attitude='Y'
像你这种采用内连接进行查询会比较慢,建议使用左外连接。
hibernate3中分组、聚合的语法也比较方便好用
使用case when then else endSELECT app.mrl,app.operationId,app.productId,
sum(case when cab.type='ONSHOW' then 1 else 0 end) as onshowNumber,
sum(case when cab.type='WITHDRAW' then 1 else 0 end) as withdrawNumber,
sum(case when cab.type='MOVE' then 1 else 0 end) as moveNumber,
sum(case when cab.type='WITHDRAW' and cab.reversion='RETURN' then 1 else 0 end) as returnNumber
from ChanageApply app, ChanageCabinets cab,ChanageCabinetsExecute exe
where app.id=cab.applicationId and app.id=exe.applicationId
and app.mrl!=null and app.mrl!=0
and app.operationId!=null and app.operationId!=0
and app.productId!=null and app.productId!=0
and exe.attitude='Y'
group by app.mrl,app.operationId,app.productId
不过是7楼提到case when我才知道hibernate3中还有这种语法
问题早已解决。
没人给出满意答案
遗憾结贴!
可以使用 LEFT JOIN 来提高查询速度。但是 Hibernate 中如果在实体映射中没有关联是不能使用 LEFT JOIN 的。该说的都已经说完了。希望楼主不要强求在 HQL 上。