gpt4 book ai didi

java - Hibernate:使用集合条件(HashSet)

转载 作者:行者123 更新时间:2023-11-30 09:43:46 25 4
gpt4 key购买 nike

我有以下类(class):

  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 创建条件?或者

  • 也许我应该使用 hql 查询
  • 也许我可以只编辑 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");
}

但我不明白它到底做了什么。什么是 resultTransformer?

拜托,任何建议或提示都会有很大帮助!谢谢!

最佳答案

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());
}

这需要 Clip/Folder 关联是双向的(即您需要在 Clip 中有一个 folder 字段)。如果不是这种情况,则可以像这样创建分离条件:

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"));
}

关于java - Hibernate:使用集合条件(HashSet),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8215211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com