HQL recursion, how do I do this?

前端 未结 3 1417
再見小時候
再見小時候 2020-12-03 14:20

I have a tree structure where each Node has a parent and a Set children. Each Node has a String title, and I want to make

3条回答
  •  没有蜡笔的小新
    2020-12-03 15:00

    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.

提交回复
热议问题