我正在尝试使用复合键映射ManyToOne关系。

我希望能够从RecoverySetup访问OnesolPeNames域对象,就像recoverySetup.onesolPeNames.peNameU
我已将以下代码添加到我的RecoverySetup映射中。

        oneSolution {
                column name: 'division'
                column name: 'peid'
            };

表A
class RecoverySetup implements Serializable {

    static constraints = {

    }

    static mapping = {
        table "recovery_setup"

        id composite: ["division", "peid", "orgkey"]

        columns{
            division column: 'division'
            peid column: 'peid'
            orgkey column: 'org_key'

            oneSolution {
                    column name: 'division'
                    column name: 'peid'
                };
        }
    }

    String division
    String peid
    String orgkey
    OnesolPeNames oneSolution

}

表B
class OnesolPeNames implements Serializable {

    static constraints = {

    }

    static mapping = {
        table "ONESOL_pe_names"

        id composite: ["division", "peid"]

        columns{
            division column: 'division', length: 8, sqlType: "char"
            peid column: 'pe_id', length: 12, sqlType: "char"
            peNameU column: 'pe_name_u', length: 50, sqlType: "char"
        }
    }

    static hasMany = [recoverySetups: RecoverySetup]

    String division
    String peid
    String peNameU

}

我得到以下异常



我也发现了这个帖子One-to-Many With Composite Keys and Different Column Names in Grails

最佳答案

由GUL Foreign key (FK_ must have same number of columns as the referenced primary key解决

RecoverySetup类实现Serializable {

static mapping = {
    table "recovery_setup"
    id composite: ["division", "peid", "orgkey"]
    columns {
        orgkey column: 'org_key', length: 8, sqlType: "char"

        oneSolName {
            column name: 'division'
            column name: 'peid'
        }
    }
    oneSolName updateable: false, insertable: false
}

static belongsTo = [oneSolName: OnesolPeNames]
...

}

07-24 15:22