简体   繁体   English

外键列表的休眠条件

[英]Hibernate criteria for a foreign key list

I have two entities: 我有两个实体:

public class Document implements java.io.Serializable {
    private Long id;
    private String info;
    private Set<Tag> tags = new HashSet<Tag>(0);
}

public class Tag implements java.io.Serializable {
    private Long id;
    private String name;
    private Set<Document> documents = new HashSet<Document>(0);
}

A document may have more than one tag, and each tag can contain many items. 一个文档可能有多个标签,并且每个标签可以包含许多项目。 Now I want to do a filter function to find out all the documents that has both tag1(id = 1) and tag2(id = 2) . 现在,我想执行一个过滤功能,以找出同时具有tag1(id = 1)tag2(id = 2)所有文档。

I tried to use these restrictions: 我尝试使用以下限制:

Criteria criteria = session.createCriteria(Document.class, "doc")
                           .createAlias("doc.tags", "tag");

List<Document> docList = criteria.add(Restrictions.eq("tag.id", 1))
                                 .add((Restrictions.eq("tag.id", 2)).list();

but they're not working, the list is empty. 但它们不起作用,列表为空。 Is there a good solution? 有没有好的解决方案?

您正在寻找id等于1 2的标签。不可能使用Restrictions.in("tag.id", Arrays.asList(1, 2))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM