简体   繁体   中英

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) .

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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