查询从一对多Grails域返回格式错误的对象

时间:2015-06-24 05:51:40

标签: grails gorm

考虑这两个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",
                ...
            },
            ...
        ]
    },
    ...
]

1 个答案:

答案 0 :(得分:0)

在您的协议课程中

static belongsTo = [agency: CollectingAgency]

将在您的班级中创建“代理”字段。您看到的agency_id只是将您的“代理商”字段映射到数据库中的“代理商”列。