Primefaces Tree StackOverFlow Exception

时间:2014-10-05 08:09:26

标签: jsf primefaces tree

我已经使用标准递归树代码从DB填充树。但是我收到此错误并且页面没有打开。

Caused by: java.lang.StackOverflowError
   at org.primefaces.model.TreeNodeChildren.add(TreeNodeChildren.java:40)
   at org.primefaces.model.TreeNodeChildren.add(TreeNodeChildren.java:22)
   at org.primefaces.model.DefaultTreeNode.<init>(DefaultTreeNode.java:59)
   at com.mesutemre.kitapislemleri.KitapListeTreeBean.createTree(KitapListeTreeBean.java:55)

我在maneged Bean中使用了这些代码;

private static final long serialVersionUID = 1L;

@EJB
private KitaplarFacade service;

TreeNode root;
TreeNode yazarAd;
private static List<Kitaplar> entityList = new ArrayList<Kitaplar>();
private static List<Kitaplar> araEntitylist;
private Kitaplar kitap;
private List<Kitaplar> subEntitylist;

public KitapListeTreeBean() {
    root = new DefaultTreeNode("Root", null);
    yazarAd = new DefaultTreeNode("", root);
}

@PostConstruct
public void initialize() {
    entityList = service.findAllKitaplar();
    createTree(entityList, 10, yazarAd);
}

private void createTree(List<Kitaplar> liste, int id, TreeNode node) {
    subEntitylist = subKitaplar(id);
    for (Kitaplar k : subEntitylist) {
        TreeNode childNode = new DefaultTreeNode(k.getKitapad(), node);
        createTree(subEntitylist, k.getKitapID(), childNode);
    }
}

public static List<Kitaplar> subKitaplar(int i) {
    araEntitylist = new ArrayList<Kitaplar>();
    for (Kitaplar k : getEntityList()) {
        if (k.getKitapID() == i) {
            araEntitylist.add(k);
        }
    }
    return araEntitylist;
}

public static List<Kitaplar> getEntityList() {
    return entityList;
}

public static void setEntityList(List<Kitaplar> entityList) {
    KitapListeTreeBean.entityList = entityList;
}

public TreeNode getRoot() {
    return root;
}

public void setRoot(TreeNode root) {
    this.root = root;
}

}

为了获取数据,我使用了JPA。为什么我收到此错误?是关于服务器设置还是我的代码?

1 个答案:

答案 0 :(得分:1)

你应该创建一个新变量,例如childID,以便解决递归永不结束。

class Kitaplar {

    private int kitapID;
    private int childID;

    public Kitaplar(int kitapID, int childID){
        this.kitapID = kitapID;
        this.childID = childID;
    }

    public int getChildID() {
        return childID;
    }

    public void setChildID(int childID) {
        this.childID = childID;
    }

    public int getKitapID() {
        return kitapID;
    }

    public void setKitapID(int kitapID) {
        this.kitapID = kitapID;
    }

}

主要课程

public class App {
    public static List<Kitaplar> dummy;

    public static void main(String[] args) {
        dummy = new ArrayList<Kitaplar>();
        dummy.add(new Kitaplar(10, 11));
        dummy.add(new Kitaplar(11, 12));
        dummy.add(new Kitaplar(12, 13));

        createTree(10);
    }

    public static void createTree(int childID) {
        List<Kitaplar> subEntitylist = subKitaplar(childID);
        for (Kitaplar k : subEntitylist) {
            createTree(k.getChildID());
        }
    }

    public static List<Kitaplar> subKitaplar(int i) {
        List<Kitaplar> araEntitylist = new ArrayList<Kitaplar>();
        for (Kitaplar k : dummy) {
            if (k.getKitapID() == i) {
                araEntitylist.add(k);
            }
        }
        return araEntitylist;
    }
}