Grails删除具有OutOfMemory的级联树

时间:2015-10-15 11:55:46

标签: java grails gorm

当我尝试删除级联时出现异常:

 2015-10-15 13:40:31,823 [http-bio-8080-exec-3] ERROR StackTrace  - Full  Stack Trace:java.lang.reflect.InvocationTargetException
    at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:198)
    at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
    at com.linkedin.grails.profiler.ProfilerFilter.doFilterInternal(ProfilerFilter.java:82)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:701)

引起:java.lang.StackOverflowError

我执行以下操作:

 TreeNode.executeUpdate("delete TreeNode m")

这是我的TreeNode域类:

class TreeNode {

TreeNode parent;

static hasMany = [children : TreeNode, elementsIndex: TreeNode]
static belongsTo = [parent: TreeNode]

static constraints = {
   name(nullable: true)
   port(nullable: true)
   nodeData(nullable: true)
   parent(nullable: true)
}

如何进行删除级联?

1 个答案:

答案 0 :(得分:0)

我通过创建一个逐个删除元素的递归函数来解决我的问题:

TreeNode deleteTreeNodeParent(TreeNode n) {
    TreeNode node = n
    if (node != null) {
        TreeNode parent = node.parent
        if (parent != null) {
            deleteTreeNodeParent(parent)
        } else {
            node.delete()
        }
    }

}

我还删除了TreeNode类中的以下行:

static belongsTo = [parent: TreeNode]