0

我有一个使用 JSF primefaces 和 EJB 托管 Bean 和休眠存储的项目。 http://www.primefaces.org/showcase/ui/treeAjax.jsf

问题:我不断收到内存不足或堆栈溢出错误,你能看出我的递归有问题吗?

@ManagedBean(name = "categoryController")
@RequestScoped
public class CategoryController implements Serializable {
    ...

    @EJB
    private CategoryFacade ejbFacade;
    private TreeNode root;

    public TreeNode getRoot() {
        return root;
    }

    @PostConstruct
    public void init() {
        root = new DefaultTreeNode("Root", null);
        System.out.print("Facade value=" + getFacade());
        Category categoryRoot = getFacade().find(new Integer(1));
        getSubcategories(categoryRoot, root);

    }

    private void getSubcategories(Category categoryRoot, TreeNode root) {
        List<Category> list = getFacade().findByNamedQuery("Category.findByPCatid", "pcatid", categoryRoot);
        Iterator<Category> it = list.iterator();
        while (it.hasNext()) {
            Category subcategory = it.next();
            TreeNode nextNode = new DefaultTreeNode(subcategory.getUcatid(), root);
            getSubcategories(subcategory, nextNode);

        }

    }

    ...
}
4

1 回答 1

0

问题是您的递归在根元素上处于无限循环中。尝试添加这个:

if (!subcategory.getCatid().equals(new Integer(1))) {
     getSubcategories(subcategory, nextNode);
}

您还可以查找 Primefaces 事件侦听器以按需生成树。 http://www.primefaces.org/showcase/ui/treeEvents.jsf

于 2012-11-07T17:43:57.557 回答