GRAILS:找到所有儿童的自我参照一对多关系 [英] GRAILS: Find all children in a self-referenced one-to-many relationship

查看:93
本文介绍了GRAILS:找到所有儿童的自我参照一对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在grails中,

如何找到一对多关系中的所有孩子,例如,

  class Employee {
static hasMany = [subordinates:Employee]
static belongsTo = [manager:Employee]
}

使用单个管理器,如何获取所有下属的下级(如遍历对象图)?

解决方案

如果您不想修改域,则递归闭包有效。否则,您可以在此示例中向 Employee 域类添加临时属性,如 allSubordinates

  class Employee {
字符串名称
static hasMany = [subordinates:Employee]
static belongsTo = [manager:Employee ]
static transients = ['allSubordinates']
def getAllSubordinates(){
return subordinates?下属* .allSubordinates.flatten()+下属:[]
}
}



<

  import grails.test。* 

类EmployeeTests扩展GrailsUnitTestCase {
雇员ceo
雇员middleManager1,middleManager2
雇员e1,e2,e3,e4,e5,e6

保护void setUp(){
super.setUp()
ceo =新员工(名称:CEO)
middleManager1 =新员工(名称:中间经理1)
e1 =新员工名称:e1)
e2 =新员工(名称:e2)
e3 =新员工(名称:e3)
middleManager2 =新员工(名称: 2)
e4 =新员工(姓名:e4)
e5 =新员工(姓名:e5)
e6 =新员工(姓名:e6)

ceo.subordinates = [middleManager1,middleMan ager2]
middleManager1.subordinates = [e1,e2,e3]
middleManager2.subordinates = [e4,e5,e6]
assert ceo.save()
}

void testAllSubordinates(){
def topLevelManager = Employee.get(ceo.id)
assertNotNull(topLevelManager);
assertEquals(8,topLevelManager.allSubordinates?.size())
}
}


In grails,

How would one find all the children in a one-to-many relationship e.g.,

class Employee {
    static hasMany = [ subordinates: Employee ]
    static belongsTo = [ manager: Employee ]
}

Using a single manager, how would one get the subordinates of all subordinates (like traversing a object graph)?

解决方案

The recursive closure works if you don't want to modify the domain. Otherwise you could add a transient property to the Employee domain class like allSubordinates in this example:

class Employee {
    String name
    static hasMany = [ subordinates: Employee ]
    static belongsTo = [ manager: Employee ]
    static transients = ['allSubordinates']
    def getAllSubordinates() {
        return subordinates ? subordinates*.allSubordinates.flatten() + subordinates : []
    }
}

Here is an integration test to see it in action:

import grails.test.*

class EmployeeTests extends GrailsUnitTestCase {
    Employee ceo
    Employee middleManager1, middleManager2
    Employee e1, e2, e3, e4, e5, e6

    protected void setUp() {
        super.setUp()
        ceo = new Employee(name:"CEO")
            middleManager1 = new Employee(name:"Middle Manager 1")
                e1 = new Employee(name:"e1")
                e2 = new Employee(name:"e2")
                e3 = new Employee(name:"e3")
            middleManager2 = new Employee(name:"Middle Manager 2")
                e4 = new Employee(name:"e4")
                e5 = new Employee(name:"e5")
                e6 = new Employee(name:"e6")

        ceo.subordinates = [middleManager1, middleManager2]
        middleManager1.subordinates = [e1,e2,e3]
        middleManager2.subordinates = [e4,e5,e6]
        assert ceo.save()
    }

    void testAllSubordinates() {
        def topLevelManager = Employee.get(ceo.id)
        assertNotNull(topLevelManager);
        assertEquals(8, topLevelManager.allSubordinates?.size())
    }
}

这篇关于GRAILS:找到所有儿童的自我参照一对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆