criteria.list() 返回重复数据

Hibernate 查询重复数据解决方案
本文介绍了一个关于 Hibernate 查询中出现重复记录的问题,并提供了解决方案。通过使用 criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY) 方法可以有效去除重复的记录。

今天碰到一个hibernate查询的问题, 在用criteria.list()的时候,返回了重复的数据,这里的重复不是指单个字段相同想过滤,而是真正的重复,完全相同的2条记录

 

解决方法:

添加 criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); 

就可以得到不重复的了

 

public PaginationSupport searchPgReq(LcdpRequestMast criteria, PaginationSupport paginationSupport) { StringBuffer sql = new StringBuffer("select a.id,a.lcdpNo, a.appType, a.workflowStartDate ,a.applicant,c.vendorName, a.poNo,a.status " + "from LcdpRequestMast a, PoMast b, VendorGen c " + "where a.poNo=b.poNo and b.vendorCode=c.vendorCode"); List paramList = new ArrayList(); if (TextUtils.stringSet(criteria.getAppNo())) { if (criteria.getAppNo().indexOf("*") > -1) { sql.append(" and a.lcdpNo like ?"); paramList.add(criteria.getAppNo().replace('*', '%').trim().toUpperCase()); } else { sql.append(" and to_char(a.lcdpNo)=?"); paramList.add(criteria.getAppNo().trim().toUpperCase()); } } if (TextUtils.stringSet(criteria.getAppType())) { sql.append(" and a.appType=?"); paramList.add(criteria.getAppType()); } if(criteria.getAppDateFrom()!=null){ sql.append(" and trunc(a.workflowStartDate)>=?"); paramList.add(criteria.getAppDateFrom()); } if(criteria.getAppDateTo()!=null){ sql.append(" and trunc(a.workflowStartDate)<=?"); paramList.add(criteria.getAppDateTo()); } if (criteria.getDelivDateFrom()!=null || criteria.getDelivDateTo()!=null) { sql.append(" and exists(from PoDetl pl where pl.poNo=a.poNo"); } if(criteria.getDelivDateFrom()!=null){ sql.append(" and to_char(pl.delivDate,'yyyy-MM-dd')>=?"); paramList.add(LcdpUtil.formDate(criteria.getDelivDateFrom(), "yyyy-MM-dd")); } if (criteria.getDelivDateTo() != null) { sql.append(" and to_char(pl.delivDate,'yyyy-MM-dd')<=?"); paramList.add(LcdpUtil.formDate(criteria.getDelivDateTo(), "yyyy-MM-dd")); } if (criteria.getDelivDateFrom()!=null || criteria.getDelivDateTo()!=null) { sql.append(")"); } if (TextUtils.stringSet(criteria.getPoNo())) { if (criteria.getPoNo().indexOf("*") > -1) { sql.append(" and a.poNo like ?"); paramList.add(criteria.getPoNo().replace('*', '%').trim().toUpperCase()); } else { sql.append(" and a.poNo=?"); paramList.add(criteria.getPoNo().trim().toUpperCase()); } } if (TextUtils.stringSet(criteria.getVendorCode())) { if (criteria.getVendorCode().indexOf("*") > -1) { sql.append(" and b.vendorCode like ?"); paramList.add(criteria.getVendorCode().replace('*', '%').trim()); } else { sql.append(" and b.vendorCode=?"); paramList.add(criteria.getVendorCode().trim()); } } if (TextUtils.stringSet(criteria.getApplicant())) { sql.append(" and a.applicant=?"); paramList.add(criteria.getApplicant()); } //for ViewLcdpReqAction security if (criteria.getId() != null) { sql.append(" and a.id=?"); paramList.add(criteria.getId()); } if (RemoteUser.get().getUserLoginId().equals("E007018") || RemoteUser.get().getUserLoginId().equals("E007599")){ sql.append(" and (exists(from LcdpProcessLog lg where lg.workflowEntry=a )"); List companyCodes = securityService.getGrantedFullSiteList(LcdpUtil.getEmpAndProposerList(), "LCDP_SEARCH"); if(companyCodes!=null && companyCodes.size()>0) sql.append(" or b.companyCode in ('"+ StringUtils.collectionToDelimitedString(companyCodes, "','") +"')"); }else { sql.append(" and (exists(from LcdpProcessLog lg where lg.workflowEntry=a and lg.caller=? or lg.proposer=? )"); paramList.add(RemoteUser.get().getUserLoginId()); paramList.add(RemoteUser.get().getUserLoginId()); List companyCodes = securityService.getGrantedFullSiteList(LcdpUtil.getEmpAndProposerList(), "LCDP_SEARCH"); if(companyCodes!=null && companyCodes.size()>0) sql.append(" or b.companyCode in ('"+ StringUtils.collectionToDelimitedString(companyCodes, "','") +"')"); } sql.append(") and a.lcdpNo is not null order by a.lcdpNo desc"); PaginationSupport list = find(sql.toString(), paramList.toArray(), paginationSupport); return list 解释代码
09-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值