GRAILS: Find all children in a self-referenced one

2019-02-07 11:50发布

问题:

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)?

回答1:

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())
    }
}


回答2:

 //Make a recursive closure
 def printAll

 printAll = { emp ->
    subordinates.each { 
                        println it
                        printAll emp
                      }
 }