views:

1218

answers:

2

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

+2  A: 
 //Make a recursive closure
 def printAll

 printAll = { emp ->
    subordinates.each { 
                        println it
                        printAll emp
                      }
 }
Sean A.O. Harney
+5  A: 

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