HQL查询存在于列表元素之间

时间:2012-11-26 00:24:26

标签: java hibernate hql

我有一个像这样的Tag类:

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语句?

1 个答案:

答案 0 :(得分: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);