Grails get child domain objects

前端 未结 4 740
陌清茗
陌清茗 2020-12-01 04:40

I have two domain classes one is parent and other one is child and i have a hasMany relationship between them. Parent class has many childs and child class belongs to parent

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 05:21

    The issue is with the use of default JSON converter. Here are your options:

     1. Default  -  all fields, shallow associations
        a. render blah as JSON
    
     2. Global deep converter - change all JSON converters to use deep association traversal
        a. grails.converters.json.default.deep = true
    
     3. Named config marshaller using provided or custom converters
        a. JSON.createNamedConfig('deep'){
            it.registerObjectMarshaller( new DeepDomainClassMarshaller(...) )
        }
        b. JSON.use('deep'){
            render blah as JSON
        }
    
     4. Custom Class specific closure marshaller 
        a. JSON.registerObjectMarshaller(MyClass){ return map of properties}
        b. render myClassInstance as JSON
    
     5. Custom controller based closure to generate a map of properties
        a. convert(object){
            return map of properties
        }
        b. render convert(blah) as JSON
    

    You are currently using Option 1, which is default.

    The simplest you can do is use Option 2 to set global deep converter, but be aware this effects ALL domain classes in your app. Which means that if you have a large tree of associations culminating in a top level object and you try to convert a list of those top level objects the deep converter will execute all of the queries to fetch all of the associated objects and their associated objects in turn. - You could load an entire database in one shot :) Be careful.

提交回复
热议问题