<h:body>
<h:form id="form7">
<p:tree value="#{treeBeanRec.rootNode}" var="node" id="tree" widgetVar="node2">
<p:treeNode>
<h:outputText value="#{node.getGroupName()}" />
</p:treeNode>
</p:tree>
</h:form>
package beans;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
@ManagedBean(name = "treeBeanRec")
@SessionScoped
public class Group {
private TreeNode rootNode;
private Tree root;
public Tree getRoot() {
return root;
}
public void setRoot(Tree root) {
this.root = root;
}
@PostConstruct
public void init() {
// instead get root object from database
rootNode = newNodeWithChildren(root, null);
}
public TreeNode newNodeWithChildren(Tree ttParent, TreeNode parent){
TreeNode newNode= new DefaultTreeNode(ttParent, parent);
for (Tree tt : ttParent.getChildren()){
TreeNode newNode2= newNodeWithChildren(tt, newNode);
}
return newNode;
}
public TreeNode getRootNode() {
return rootNode;
}
public void setRootNode(TreeNode node) {
rootNode = node;
}
}
------------------------------------------------
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.*;
import javax.persistence.Id;
import javax.persistence.EntityManager;
@Entity
@Table(name = "group" )
public class Tree implements Serializable {
private static final long serialVersionUID = 1L;
private EntityManager entityManager;
List<Tree> TreeVar;
public List<Tree> getTreeVar() {
return entityManager.createQuery("select c FROM Tree c").getResultList();
}
public List<Tree> getAllTree() {
return entityManager.createQuery("select c FROM Tree c").getResultList();
}
public EntityManager getEntityManager() {
return entityManager;
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
public Tree() {
}
@Id
@Column(name = "ID", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getGroupName() {
return GroupName;
}
public void setGroupName(String GroupName) {
this.GroupName = GroupName;
}
public List<Tree> getChildren() {
return children;
}
public void setChildren(List<Tree> children) {
this.children = children;
}
@Column(name = "GroupName")
private String GroupName ;
@OneToMany
@JoinColumn(name="PARENTID")
public List<Tree > children = new LinkedList<Tree >();