这是我最终使用HQL实现它的方式:
public List<DriversLicence> findDriversLicencesWith(List<LicenceClass> licenceClasses) { String hqlString = "select dl from DriversLicenceImpl dl where 1=1 "; for (int i = 0; i < licenceClasses.size(); i++) { hqlString += " and :licenceClass" + i + " = some elements(dl.licenceClasses)"; } Query query = getSession().createQuery(hqlString); for (int i = 0; i < licenceClasses.size(); i++) { query.setParameter("licenceClass" + i, licenceClasses.get(i)); } return query.list();}
或通过sqlRestriction使用Hibernate Criteria:
for (LicenceClass licenceClass : licenceClasses) { criteria.add(Restrictions.sqlRestriction("? = some(select " + LicenceClass.PRIMARY_KEY + " from " + LICENCE_CLASS_JOIN_TABLE + " where {alias}." + DriversLicence.PRIMARY_KEY + " = " + DriversLicence.PRIMARY_KEY + ")", licenceClass.getId(), Hibernate.LONG));}
LICENCE_CLASS_JOIN_TABLE是hibernate生成的表的名称,该表支持driverLicence和LicenceClass之间的多对多关系。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)