hibernate实现表联查并获取某个字段的最大值(随笔)

来源:互联网 发布:excel横纵数据互换 编辑:程序博客网 时间:2024/06/05 20:50

最近试用hibernate实现三表连接查询并分组显示某个字段的最大值,在mysql中语句可以运行,改成hql无法运行,遂写下随笔以记录

mysql中查询,可以查询到相应的结果

改写成hql语句

"select o.equipment.equId, o.olderName , e1.power, max(opr.oTime),e1.location from Older o ,Equipment e1 , OperationRecord opr whereopr.equipment.equId = o.equipment.equId = e1.equId and e1.equId in(select e2.equId from Equipment e2 where e2.user.userId ="+userId+")group by o.equipment.equId,opr.equipment.equId" ;

运行程序会报如下错误:


之前以为是字段名没有对应上,仔细查看,没有问题,然后又以为是hibernate中需要给函数(max)取别名,依然报错,经多次尝试修改,将上面hql红字部分改写为蓝字标记部分:

"select o.equipment.equId, o.olderName , e1.power, max(opr.oTime),e1.location from Older o ,Equipment e1 , OperationRecord opr whereopr.equipment.equId = e1.equId and o.equipment.equId = e1.equIdand e1.equId in(select e2.equId from Equipment e2 where e2.user.userId ="+userId+")group by o.equipment.equId,opr.equipment.equId"

然后可以运行显示如下结果:


遂写下个人总结:hibernate可能不支持多等号连接,需要将其进行拆分以and连接,纯属个人言论,仅供参考。。。。

0 0
原创粉丝点击