title: Building primefaces tree from database using recursion
body: <p>I am working with primefaces tree component. I am facing problems with constructing the tree from Database using recursion. I had a look at this question <a href="https://stackoverflow.com/questions/11189697/primefaces-tree-from-database">Primefaces tree from database</a> . Still, I am not able to build the tree. 
  I have a getChildNodes list in the for loop that is getting called repeatedly as it is recursive function. I am not able to fix it. Below is my code.</p>
  
  <p>Entity</p>
  
  <pre><code>package com.model;
  
  import java.io.Serializable;
  import javax.persistence.*;
  import javax.persistence.Id;
  
  
  /**
   * The persistent class for the tree database table.
   * 
   */
  @Entity
  @Table(name = "TREE")
  public class Tree implements Serializable {
      private static final long serialVersionUID = 1L;
  
      @Id
      @Column(name="NODE_ID")
      private int nodeId;
  
      @Column(name="NODE_DATA")
      private String nodeData;
  
      @Column(name="PARENT_ID")
      private int parentId;
  
      @Column(name="NODE_TYPE")
      private String nodeType;
  
      public Tree() {
      }
  
      public int getNodeId() {
          return this.nodeId;
      }
  
      public void setNodeId(int nodeId) {
          this.nodeId = nodeId;
      }
  
      public String getNodeData() {
          return this.nodeData;
      }
  
      public void setNodeData(String nodeData) {
          this.nodeData = nodeData;
      }
  
      public int getParentId() {
          return this.parentId;
      }
  
      public void setParentId(int parentId) {
          this.parentId = parentId;
      }
  
      public String getNodeType() {
          return nodeType;
      }
  
      public void setNodeType(String nodeType) {
          this.nodeType = nodeType;
      }
  
  
  }
  </code></pre>
  
  <p>ManagedBean</p>
  
  <pre><code>package com.managedBeans;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import javax.annotation.PostConstruct;
  
  import org.primefaces.model.DefaultTreeNode;
  import org.primefaces.model.TreeNode;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Scope;
  import org.springframework.stereotype.Component;
  
  import com.model.Tree;
  import com.service.TreeService;
  
  /**
   * @author ADMIN
   * 
   */
  
  @Component("treeBeanRecursive")
  @Scope("request")
  public class TreeBeanRecursive {
  
      private TreeNode rootNode;
  
      @Autowired
      TreeService treeService;
  
      private List&lt;Tree&gt; childNodes;
  
      public TreeService getTreeService() {
          return treeService;
      }
  
      public void setTreeService(TreeService treeService) {
          this.treeService = treeService;
      }
  
      public TreeBeanRecursive() {
  
      }
  
      @PostConstruct
      public void init() {
  
          Tree root = treeService.getRoot();
  
          childNodes = treeService.getChildNodes();
  
          rootNode = getNodeWithChildren(root, null);
  
      }
  
      public TreeNode getNodeWithChildren(Tree t1, TreeNode parent){
          TreeNode newNode= new DefaultTreeNode(t1, parent);
  
          for (Tree tt : childNodes){
               TreeNode newNode2= getNodeWithChildren(tt, newNode);
          }
  
          return newNode;
     }
  
  
  
      public TreeNode getRootNode() {
          return rootNode;
      }
  
      public void setRootNode(TreeNode node) {
          rootNode = node;
      }
  
      public void showMessage() {
          treeService.showMessage();
      }
  
      public List&lt;Tree&gt; getChildNodes() {
          return childNodes;
      }
  
      public void setChildNodes(List&lt;Tree&gt; childNodes) {
          this.childNodes = childNodes;
      }
  
  }
  </code></pre>
  
  <p>childNodes is the list of all nodes except the root node. I am getting the list from spring service treeService.getChildNodes();</p>
  
  <p>tree.xhtml</p>
  
  <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui"&gt;
  &lt;h:head&gt;
      &lt;link type="text/css" rel="stylesheet" href="themes/bluesky/skin.css" /&gt;
      &lt;style type="text/css"&gt;
  .ui-panel {
      font-size: 85% !important;
      font-family: calibri !important;
      align: "center" !important;
  }
  
  .customProjectNode {
      background-image: url(./../images/tree/root.png) !important;
      width: 17px;
      height: 16px;
  }
  &lt;/style&gt;
  &lt;/h:head&gt;
  &lt;h:body&gt;
  
  
      &lt;h:form id="form"&gt;
  
          &lt;p:tree value="#{treeBeanRecursive.rootNode}" var="node" id="tree"&gt;
              &lt;p:treeNode&gt;
                  &lt;h:outputText value="#{node.getNodeData()}" /&gt;
              &lt;/p:treeNode&gt;
              &lt;p:treeNode type="#{node.getNodeType()}" icon="customProjectNode"&gt;
                  &lt;h:outputText value="#{node.getNodeData()}" /&gt;
              &lt;/p:treeNode&gt;
          &lt;/p:tree&gt;
  
  
      &lt;/h:form&gt;
  
  &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>My table has the following data. Root node parent is 0. </p>
  
  <p><strong>NodeID             Data                      ParentID                       nodeType</strong></p>
  
  <p>'1',                'Project Name',                '0',                     'ProjectName'</p>
  
  <p>'2',                 'Node1',                      '1',                     'hardware'</p>
  
  <p>'3',                 'Node2',                      '1',                     'software'</p>
  
  <p>'4',                 'Node3',                      '1',                     'software'</p>
  
  <p>'5',                 'Node4',                      '2',                     'hardware'</p>