GRAILS: Find all children in a self-referenced one-to-many relationship

后端 未结 2 729
长情又很酷
长情又很酷 2021-01-01 06:08

In grails,

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

class Employee {
    static hasMany = [ subordinates: Employee ]
          


        
相关标签:
2条回答
  • 2021-01-01 06:20

    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())
        }
    }
    
    0 讨论(0)
  • 2021-01-01 06:32
     //Make a recursive closure
     def printAll
    
     printAll = { emp ->
        subordinates.each { 
                            println it
                            printAll emp
                          }
     }
    
    0 讨论(0)
提交回复
热议问题