我有以下课程:
public class Folder{
private Set<Documents> documents;
private Set<Clip> clips;
}
public class Clip{
private Owner owner;
}
我需要在所有者的黄色 文件夹中找到漂亮 文档的列表这些文件夹中的 剪辑,名称就像搜索字符串一样。像这样(不工作):
Criteria criteria = session.createCriteria(Document.class);
criteria.add(Restrictions.eq("isNice", 1));
criteria.createCriteria("folder").add(Restrictions.eq("isYellow", 1));
criteria.createCriteria("clips");
criteria.createCriteria("owner").add(Restrictions.like("name", search));
List documents = criteria.list();
如何为HashSet创建条件? 或者
List documents
,删除错误的条目?修改。我找到了这段代码:
List cats = sess.createCriteria(Cat.class)
.createCriteria("kittens", "kt")
.add( Restrictions.eq("name", "F%") )
.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP)
.list();
Iterator iter = cats.iterator();
while ( iter.hasNext() ) {
Map map = (Map) iter.next();
Cat cat = (Cat) map.get(Criteria.ROOT_ALIAS);
Cat kitten = (Cat) map.get("kt");
}
但我无法理解它究竟是做什么的。什么结果变形金刚?
请,任何建议或提示都会有所帮助!谢谢!
答案 0 :(得分:2)
Criteria criteria = session.createCriteria(Document.class, "document");
criteria.add(Restrictions.eq("document.nice", true));
criteria.createAlias("document.folder", "folder");
criteria.add(Restrictions.eq("folder.yellow", true));
criteria.add(Subqueries.exists(clipOwnerWithNameInFolder(name, "folder"));
private DetachedCriteria clipOwnerWithName(String searchedName, String aliasOfFolder) {
DetachedCriteria c = DetachedCriteria.forClass(Clip.class, "clip");
c.createAlias("clip.owner", "owner");
c.createAlias("clip.folder", "clipFolder");
c.add(Restrictions.propertyEq("clipFolder.id", aliasOfFolder + ".id");
c.add(Restrictions.like("owner.name", searchedName);
c.setProjection(Projections.id());
}
这需要剪辑/文件夹关联是双向的(即您需要folder
中有Clip
字段。如果不是这样,可以像这样创建分离的标准:
private DetachedCriteria clipOwnerWithName(String searchedName, String aliasOfFolder) {
DetachedCriteria c = DetachedCriteria.forClass(Folder.class, "clipFolder");
c.createAlias("clipFolder.clips", "clip");
c.createAlias("clip.owner", "owner");
c.add(Restrictions.propertyEq("clipFolder.id", aliasOfFolder + ".id");
c.add(Restrictions.like("owner.name", searchedName);
c.setProjection(Projections.property("clip.id"));
}