0

我有一个这样的标签类:

public class Tag {

    private Long id;

    private String title;
    private Set<Document> documents = new HashSet<Document>();

    public Tag(){}

//getter and setter...

}

还有一个由一些标签对象分类的类文档

public class Document {

    private Long id;

    private String title;
    private Int numberPages;
    private Set<Tag> tags = new HashSet<Tag>();

    public Document(){}

//getter and setter...

}

现在我想获得最多 10 页的文档,它们有标签“短”或“论文”

我猜是这样的:

int nPages = 10;
Query query = session.createQuery("from Document where nPages <= :numberPages and (<<short>> OR <<essay>>);  
query.setInteger("numberPages", nPages);  

标签列表可以是可变的。我想区分何时必须是所有标签(标签之间的“AND”)或任何标签就足够了(标签之间的“OR”)

如何构建 sql 语句?

4

1 回答 1

2

我认为你HQL应该如下所示:

  Query query = session.createQuery(
           "from Document d join d.tags t "+
           " where d.nPages <= :numberPages and "+
              " t.title in (:titles) "; 
  List<String> titles = new ArrayList<String>();
  titles.add("short");
  titles.add("essay");
  query.setParameter("numberPages", nPages);  
  query.setParameterList("titles", titles);  
于 2012-11-26T00:53:33.280 回答