简体   繁体   English

数据库中的Primefaces树

[英]Primefaces tree from database

I have the following entity class : 我有以下实体类:

@Entity
@Table(name = "THE_TREE", catalog = "", schema = "dbo")
public class TheTree implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name = "NODE_NAME")
private String name;

@Column(name = "LEVEL")
private int level;

@OneToMany    
@JoinColumn(name="PARENTID")    
public List<TheTree > children = new LinkedList<TheTree >();

I would like to represent this into primefaces tree, but I cant get it right. 我想将其表示为素数树,但我做对了。 The example given in primefaces website has static nodes with predefined depth, where I need nodes with unknown depth and to be filled from database. primefaces网站中给出的示例包含具有预定义深度的静态节点,在这里我需要深度未知的节点并要从数据库中填充。 I have seen various posts here but nothing is clear to me. 我在这里看到过各种帖子,但我不清楚。 In this post it seems the author has asked the same question but the answer is not relative to the question somehow. 在这篇文章中 ,作者似乎提出了相同的问题,但答案与问题无关。 Any solution would be appreciated. 任何解决方案将不胜感激。

You have to create a recursive function to make the tree. 您必须创建一个递归函数来制作树。 This is how I would do it: 这就是我要做的:

@ManagedBean
@ViewScoped
public class TreeMBean {

    private TreeNode rootNode;

     @PostConstruct
     public void init() {
         TheTree root = new TheTree(); // instead get root object from database 
         rootNode = newNodeWithChildren(root, null);
     }

     /**
      *  recursive function that returns a new node with its children
     */
     public TreeNode newNodeWithChildren(TheTree ttParent, TreeNode parent){
          TreeNode newNode= new DefaultTreeNode(ttParent, parent);
          for (TheTree tt : ttParent.getChildren()){
               TreeNode newNode2= newNodeWithChildren(tt, newNode);
          }
          return newNode;
     }

     public TreeNode getRootNode() {
         return rootNode;
     }

     public void setRootNode(TreeNode node) {
         rootNode = node;
     }

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM