StringBuffer queryString=new StringBuffer();
queryString.append("select distinct purchase from ConPurchaseInfo purchase,ConSalesInfo sales where sales.salesContractno like '%"+value+"%' and purchase in elements(sales.conPurchaseInfo) and (purchase.id in(")
.append(contractIds)
.append(") or purchase.createUserId='"+userInfo.get(Constant.USERID_SESSION)+"' ) and purchase.recycleFlag=0 and purchase.contractStatus=0");
if("user".equals(isUserOrOrg)){
queryString.append(" and purchase.purchaseChiefId='"+id+"' ");
}else if("org".equals(isUserOrOrg)){
queryString.append(" and purchase.purchaseChiefDepartmentId in("+getOrgStrs(id,true)+") ");
}else{
queryString.append(" ");
}
if(StringUtils.isNotBlank(state)){
queryString.append(" and ");
queryString.append("purchase.applicationStatus ='" + state + "'");
}
queryString.append(" order by purchase.updateTime desc,purchase.signedDate desc");
是否有过这种需求?对象之间是多对多,需要一方查询另一方,而条件是一方除了满足自身的where限制意外,还要受关联的多的一方的限制。
比如:
public class ConPurchaseInfo extends BaseEntityUUID implements ResourceEntity{
private Set<ConSalesInfo> conSalesInfos;
private static final long serialVersionUID = 1L;}
ConPurchaseInfo 中有对ConSalesInfo的关联many-to-many,
现在要根据conSalesInfo中的属性name来查询conPurchaseInfo的信息。
用sql是可以写的,现在介绍hql写法。
用到了in 和 in elements这两个不同的用法
其中in 都知道了把,in elements 是专门针对set集合的。
最简单例子hql如下:
select distinct purchase from ConPurchaseInfo purchase,ConSalesInfo sales where sales.name like '%"+value+"%' and purchasein elements(sales.conPurchaseInfo);