HQL內連接查詢,去重

因爲查詢條件需要對 連接的表中的兩個字段 or 條件查詢

導致查詢出來的記錄,有重複

一.

使用 group by 主鍵(用這種可以 再 SELECT COUNT(*)    進行分頁,記錄數的統計)

@Override
	public List<Remotecertapplyorder> getOrderList(int userid, String username,Date[] createDates)
	{
		RemotecertapplyorderDao remotecertapplyorderDao =(RemotecertapplyorderDao) dao;
		List<Date> dateValues = new ArrayList<Date>();
		//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<Remotecertapplyorder> remotecertapplyorders =
				(List<Remotecertapplyorder>) remotecertapplyorderDao.find(hql.toString(), dateValues.toArray());
		return remotecertapplyorders;
	}


上面查詢的count  與實際得到的 list 結果不一致 需要採用子查詢

-----》

@Override
	public List<Remotecertapplyorder> getOrderList(int userid, String username,Date[] createDates)
	{
		RemotecertapplyorderDao remotecertapplyorderDao =(RemotecertapplyorderDao) dao;
		List<Date> dateValues = new ArrayList<Date>();
		
		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<Remotecertapplyorder> remotecertapplyorders =
				(List<Remotecertapplyorder>) remotecertapplyorderDao.find(hql.toString(), dateValues.toArray());
		return remotecertapplyorders;
	}

這樣 count 與 得到的實際值相同





二.

使用 distinct

public PaginationSupport<Remotecertapplyorder> getorderList(int userid,     //動態查詢加分頁
			String username, Date beginTime, Date endTime,
			Integer pagesize, Integer currpageno)
	{
		RemotecertapplyorderDao remotecertapplyorderDao=(RemotecertapplyorderDao) dao;
		PaginationSupport<Remotecertapplyorder> result=new PaginationSupport<Remotecertapplyorder>();
		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()<currpageno){
				currpageno = result.getTotalPageCount();
				result.setCurrPageNo(currpageno);
			}
			List<Remotecertapplyorder> list=remotecertapplyorderDao.orderByPage(hql.toString(),result.getPageSize(),result.getCurrPageNo());
			result.setItems(list);
		}
		return result;
	}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章