grails - 来自一对多 Grails 域的查询返回的格式错误的对象

标签 grails grails-orm

考虑这两个 Grails domain类:

class Agreement implements Serializable {
    String code
    String branchCode
    ...

    static belongsTo = [agency: CollectingAgency]

    static mapping = {
        ...
    }
}

class CollectingAgency implements Serializable {
    String type
    String agencyClass
    ...

    static hasMany = [agreement: Agreement]

    static mapping = {
        ...
    }
}

现在当我执行 Agreement.findAll()它创建了一个与此类似的 sql(使用 loggingSql = true ):
select agreement0_.agency_id as agency4_67_1_, agreement0_.AGREH_CD as
    AGREH1_1_, agreement0_.AGREH_BRCHCD as AGREH2_1_,
    ...
    agreement0_.agency_id as agency4_66_0_,
                ^^^^^^^^^
    ...
from RVAGREHDROTB agreement0_
where agreement0_.agency_id=?

由于未知列(agency_id),该语句将不会执行。我想知道它从哪里得到 agency_id柱子?我还没有映射任何具有此类名称的列。

当我尝试使用 CollectingAgency.findAll() 从另一端查询时,它返回一个格式错误的 Object :
[
    {
        type: "0400300",
        agencyClass: "12",
        ...
        agreement: [

是的,就像那样,使用 agreement具有左方括号的键 [但没有右括号 ] .另外,不会检索表的其他属性。

如何使用类似于以下的结果对象实现查询:
Agreement.findAll() :
[
    {
        code: "1212",
        branchCode: "a014s",
        ...
        agency: {
            type: "0400300",
            agencyClass: "12",
            ...
        },
        ...
    },
    {
        code: "1213",
        branchCode: "a014z",
        ...
        agency: {
            type: "0400300",
            agencyClass: "12",
            ...
        },
        ...
    },
    ...
]

CollectingAgency.findAll() :
[
    {
        type: "0400300",
        agencyClass: "12",
        ...
        agreement: [
            {
                code: "1212",
                branchCode: "a014s",
                ...
            },
            {
                code: "1213",
                branchCode: "a014z",
                ...
            },
            ...
        ]
    },
    ...
]

最佳答案

在您的协议(protocol)类(class)中,您有

static belongsTo = [agency: CollectingAgency]

这将在您的类(class)中创建一个“机构”字段。您看到的机构 ID 只是将您的“机构”字段映射到数据库中的“机构”列。

关于grails - 来自一对多 Grails 域的查询返回的格式错误的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31018582/

相关文章:

java - ils。从远程服务器调用CURL后重定向不起作用

sql - 访问 hql 查询中的连接表以获取 grails 中的多对多关系

templates - 如何在字段插件中对相同的类使用自定义模板,grails

Grails/GORM : The meaning of belongsTo in 1:N relationships

Grails 数据库迁移 gorm diff 不会产生任何变化

mysql - 如何使用 web-api 配置文件在 Grails 3.1.x 上配置 MySQL 数据源?

ajax - Grails文件下载-客户端

unit-testing - 对(超过?)扩展使用服务的基类的 Grails 域进行单元测试

hibernate - Grails 2.0 中的 MappedSuperclass 替代方案

java - Grails 中 Quartz 工作的 withTransaction?