符合所有子集合的休眠条件 [英] Hibernate Criteria to match against all child collection
本文介绍了符合所有子集合的休眠条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Product和Tag之间有一个休眠的多对多关系
产品类别具有标签"集合,可按多对多映射进行设置
我正在尝试获取与所有标签(而不是任何标签)匹配的产品
以下将获取与任何标签匹配的所有产品.
I have a hibernate many to many relationship between Product and Tag
Product class has a 'tags' collection Set as per many to many mapping
I am trying to fetch the products which matches all the tags (not any of the tags)
the following will fetch all products which matches any of the tag.
Criteria crit = session.createCriteria(Product.class,"Prdct");
crit.createAlias("Prdct.tags","PT");
crit.add(Restrictions.in("PT.Name",selectedTags));
crit.list();
如何使用条件执行此操作?
how to do this using criteria?
推荐答案
仅对集合有用
// get the count of tags matching the criteria
DetachedCriteria subquery = DetachedCriteria.For(Product.class)
.add(Expression.eq("id","product.id"))
.createAlias("tags","tag")
.add(Restrictions.in("tag.Name", selectedTags))
.setProjection(Projections.count("PT.Name"));
// get the Products where there all tags match
Criteria crit = session.createCriteria(Product.class,"product")
.add(Subqueries.eq(selectedTags.getCount(), subquery);
这篇关于符合所有子集合的休眠条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文