gpt4 book ai didi

java - HQL递归,我该怎么做?

转载 作者:太空狗 更新时间:2023-10-29 22:53:32 25 4
gpt4 key购买 nike

我有一个树结构,其中每个 Node有一个 parent 和一个 Set<Node> children .每个节点都有一个 String title ,我想在我选择 Set<String> titles 的地方进行查询,是该节点和所有父节点的标题。如何编写此查询?

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

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

干杯

尼克

最佳答案

您不能使用 HQL 进行递归查询。 See this .正如那里所说,它甚至不是标准的 SQL。您有两个选择:

  • 编写特定于供应商的递归 native SQL query
  • 进行多次查询。例如:

    // 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
    }

我肯定会选择第二个选项。

关于java - HQL递归,我该怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2486757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com