Gorm使用复合键进行多对一映射

时间:2015-06-30 19:39:30

标签: hibernate grails gorm

我尝试使用复合键映射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

}

我收到以下异常

  

由MappingException引起:实体映射中的重复列:   org.hri.pisr.domain.RecoverySetup列:除法(应该映射   with insert =" false"更新="假&#34)

我也发现了这篇SO One-to-Many With Composite Keys and Different Column Names in Grails

1 个答案:

答案 0 :(得分:0)

由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]
...

}