Grails获取子域对象 [英] Grails get child domain objects

查看:169
本文介绍了Grails获取子域对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个域类,一个是父类,另一个是子类,我有一个hasMany关系。父类有许多子类,子类属于父类。
这里是编码示例。

pre code $> class Parent {
字符串名称
static hasMany = [childs:Child]
static constraints = {
}
}


class Child {
字符串名称
static belongsTo = [parent:Parent]
static constraints = {}
}

问题只要我得到父对象,与父类关联的子对象也被获取。但是,当我将对象转换为JSON时,我完全看不到子对象,我只能看到子对象的ID。



转换的JSON响应:

  [{class:project.Parent,id:1,
name:name1,childs:[{class:Child ,id:1},{class:Review,id:2}]}]

但是我想要包含子对象名称的响应,如下所示:

  [{class: project.Parent,id:1,name:name1,
childs:[{class:Child,id:1,name:childname1 },
{class:Review,id:2,name:childname2}
]
}]

非常感谢任何帮助。
在此先感谢。

解决方案

问题在于使用默认的JSON转换器。这里有你的选择:

  1.默认 - 所有字段,浅层关联
a。渲染为JSON

2.全局深度转换器 - 将所有JSON转换器更改为使用深度关联遍历
a。 grails.converters.json.default.deep = true

3.使用提供的或自定义转换器命名的配置编组器
a。 JSON.createNamedConfig('deep'){
it.registerObjectMarshaller(new DeepDomainClassMarshaller(...))
}
b。 JSON.use('deep'){
渲染为JSON
}}

4.自定义类特定的闭包编组
a。 JSON.registerObjectMarshaller(MyClass){返回属性映射}
b。将myClassInstance渲染为JSON

5.基于自定义控制器的闭包生成属性映射
a。 convert(object){
属性返回映射
}
b。将转换(转换为)转换为JSON

您正在使用选项1,这是默认选项。

最简单的方法是使用选项2设置全局深度转换器,但请注意,这会影响应用中的所有域类。这意味着如果您有一个大型关联树顶点在顶层对象中,并且您尝试转换这些顶层对象的列表,则深层转换器将执行所有查询以获取所有关联对象及其关联对象转。 - 您可以一次加载整个数据库 :)请小心。


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 class. And here is coding example.

class Parent{
   String name
    static hasMany = [childs:Child] 
    static constraints = {
   }
}


class Child{
   String name
   static belongsTo = [parent:Parent]
   static constraints={}
}

Problem is as soon as I get the parent object the child objects associated with parent class were also fetched. But when I convert the object to JSON I don't see the child object completely I can only able to see the ID's of child objects. I want to see all columns of child object instead of only Id.

Converted JSON response:

[{"class":"project.Parent","id":1,
  "name":"name1","childs":[{"class":"Child","id":1},{"class":"Review","id":2}]}]

But I want the response which contains name of child object too, as follows

[{"class":"project.Parent","id":1,"name":"name1",
  "childs":[{"class":"Child","id":1,"name":"childname1"},
            {"class":"Review","id":2,"name":"childname2"}
           ]
}]

Any help greatly appreciated. Thanks in advance.

解决方案

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.

这篇关于Grails获取子域对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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