HQL 递归,我该怎么做?

编程入门 行业动态 更新时间:2024-10-28 02:30:05
本文介绍了HQL 递归,我该怎么做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

干杯

尼克

推荐答案

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

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

  • 编写特定于供应商的递归原生 SQL 查询
  • 进行多次查询.例如:

  • 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 递归,我该怎么做?

本文发布于:2023-10-18 02:12:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1502779.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归   怎么做   我该   HQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!