博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HQL内连接查询,去重
阅读量:4096 次
发布时间:2019-05-25

本文共 3557 字,大约阅读时间需要 11 分钟。

因为查询条件需要对 连接的表中的两个字段 or 条件查询

导致查询出来的记录,有重复

一.

使用 group by 主键(用这种可以 再 SELECT COUNT(*)    进行分页,记录数的统计)

@Override	public List
getOrderList(int userid, String username,Date[] createDates) { RemotecertapplyorderDao remotecertapplyorderDao =(RemotecertapplyorderDao) dao; List
dateValues = new ArrayList
(); //StringBuffer hql=new StringBuffer("select distinct ro from Remotecertapplyorder ro left join fetch ro.remotecertapplies r where 1=1 "); StringBuffer hql=new StringBuffer("select ro from Remotecertapplyorder ro inner join ro.remotecertapplies r where 1=1 "); hql.append(" and ro.applicant.id = "+userid); if(StringUtils.hasText(username)){ hql.append(" and ( r.username like '%"+username+"%' or r.invoicename like '%"+username+"%') "); } if(createDates[0]!=null){ hql.append(" and ro.createtime >= ? "); dateValues.add(createDates[0]); } if(createDates[1]!=null){ hql.append(" and ro.createtime <= ? "); dateValues.add(createDates[1]); } hql.append(" group by ro.id order by ro.id "); List
remotecertapplyorders = (List
) remotecertapplyorderDao.find(hql.toString(), dateValues.toArray()); return remotecertapplyorders; }

上面查询的count  与实际得到的 list 结果不一致 需要采用子查询

-----》

@Override	public List
getOrderList(int userid, String username,Date[] createDates) { RemotecertapplyorderDao remotecertapplyorderDao =(RemotecertapplyorderDao) dao; List
dateValues = new ArrayList
(); StringBuffer hql=new StringBuffer("from Remotecertapplyorder where id in (select ro.id from Remotecertapplyorder ro left join ro.remotecertapplies r where 1=1 "); hql.append(" and ro.applicant.id = "+userid); if(StringUtils.hasText(username)){ hql.append(" and ( r.username like '%"+username+"%' or r.invoicename like '%"+username+"%') "); } if(createDates[0]!=null){ hql.append(" and ro.createtime >= ? "); dateValues.add(createDates[0]); } if(createDates[1]!=null){ hql.append(" and ro.createtime <= ? "); dateValues.add(createDates[1]); } hql.append(" group by ro.id ) order by id "); List
remotecertapplyorders = (List
) remotecertapplyorderDao.find(hql.toString(), dateValues.toArray()); return remotecertapplyorders; }
这样 count 与 得到的实际值相同

二.

使用 distinct

public PaginationSupport
getorderList(int userid, //动态查询加分页 String username, Date beginTime, Date endTime, Integer pagesize, Integer currpageno) { RemotecertapplyorderDao remotecertapplyorderDao=(RemotecertapplyorderDao) dao; PaginationSupport
result=new PaginationSupport
(); if(currpageno>0) result.setCurrPageNo(currpageno); if(pagesize>0) result.setPageSize(pagesize); StringBuffer hql=new StringBuffer("select distinct ro from Remotecertapplyorder ro left join fetch ro.remotecertapplies r " + "where ro.applicant.id = "+userid ); if(username!=null && username.length()>0) hql.append(" and ( r.username like '%"+username+"%' or r.invoicename like '%"+username+"%')"); if(beginTime!=null) hql.append(" and ro.createtime >= '"+beginTime+"' "); if(endTime!=null) hql.append(" and ro.createtime <= '"+endTime+"' "); hql.append(" order by ro.id "); int count=remotecertapplyorderDao.getCount(hql.toString()); result.setTotalCount(count); if(count!=0){ if(result.getTotalPageCount()
list=remotecertapplyorderDao.orderByPage(hql.toString(),result.getPageSize(),result.getCurrPageNo()); result.setItems(list); } return result; }

转载地址:http://ohlii.baihongyu.com/

你可能感兴趣的文章
configure: error: Please reinstall the BZip2 distribution
查看>>
OpenCV gpu模块样例注释:video_reader.cpp
查看>>
【增强学习在无人驾驶中的应用】
查看>>
《python+opencv实践》四、图像特征提取与描述——29理解图像特征
查看>>
《python+opencv实践》四、图像特征提取与描述——30Harris 角点检测
查看>>
《python+opencv实践》四、图像特征提取与描述——31 Shi-Tomasi 角点检测& 适合于跟踪的图像特征
查看>>
OpenCV meanshift目标跟踪总结
查看>>
人工神经网络——神经元模型介绍
查看>>
人工神经网络——感知器介绍
查看>>
人工神经网络——反向传播算法(BackPropagation)
查看>>
进程的地址空间概述
查看>>
Windows 窗口底层原理
查看>>
一种函数指针的运用
查看>>
Win32程序之进程的原理
查看>>
C++虚函数原理
查看>>
MySQL的索引
查看>>
今天,Python信息量很大!
查看>>
Flash 已死,Deno 当立?
查看>>
编程差的程序员,90%都是吃了数学的亏!骨灰级开发:方法不对,努力也白费...
查看>>
编程差的程序员,90%都是吃了数学的亏!骨灰级开发:方法不对,努力也白费...
查看>>