簡體   English   中英

外鍵列表的休眠條件

[英]Hibernate criteria for a foreign key list

我有兩個實體:

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

一個文檔可能有多個標簽,並且每個標簽可以包含許多項目。 現在,我想執行一個過濾功能,以找出同時具有tag1(id = 1)tag2(id = 2)所有文檔。

我嘗試使用以下限制:

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

但它們不起作用,列表為空。 有沒有好的解決方案?

您正在尋找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