HQL递归,我该怎么做? [英] HQL recursion, how do I do this?

查看:107
本文介绍了HQL递归,我该怎么做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个树结构,其中每个 Node 有一个父元素,一个 Set< Node>孩子。每个节点都有一个字符串标题,我想在其中选择 Set< String>标题,是该节点和所有父节点的标题。我如何编写这个查询?



单个标题的查询就是这样,但就像我说的,我希望它扩展到整个家长分支。 / p>

  SELECT node.title FROM节点WHERE node.id =:id 

干杯



Nik

解决方案

你不能用HQL进行递归查询。 查看此。如前所述,它甚至没有标准的SQL。您有两种选择:


  • 编写供应商特定递归本机 SQL查询

  • make多个查询。例如:

      //使用查询获取第一个节点
    while(currentNode.parent!= null){
    Query q = //创建查询
    q.setParameter(id,currentNode.getParentId());
    Node currentNode =(Node)q.getSingleResult();
    nodes.add(currentNode); //这是Set
    }




<我肯定会选择第二种方式。


I have a tree structure where each Node has a parent and a Set<Node> children. Each Node has a String title, and I want to make a query where I select Set<String> titles, being the title of this node and of all parent nodes. How do I write this query?

The query for a single title is this, but like I said, I'd like it expanded for the entire branch of parents.

SELECT node.title FROM Node node WHERE node.id = :id

Cheers

Nik

解决方案

You can't do recursive queries with HQL. See this. And as stated there it is not even standard SQL. You have two options:

  • write a vendor-specific recursive native SQL query
  • make multiple queries. For example:

    // obtain the first node using your query
    while (currentNode.parent != null) {
       Query q = //create the query
       q.setParameter("id", currentNode.getParentId());
       Node currentNode = (Node) q.getSingleResult();
       nodes.add(currentNode); // this is the Set
    }
    

I'd definitely go for the 2nd option.

这篇关于HQL递归,我该怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆