外键(FK_必须与引用的主键具有相同的列数

时间:2015-07-01 16:45:24

标签: hibernate grails gorm

我正在使用遗留数据库,其中表A由3个复合键组成,表B由2个复合键组成,这两个复合键与表A中的两个复合键相同

OnesolPeNames

RecoverySetup

class OnesolPeNames implements Serializable {

    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]

    ....

我正试图像这样访问OnesolPeNames。

recoverySetup.onesolPeNames.peNameU。

我收到以下错误

  

由MappingException引起:外键   (FK_ib9w9pn893cwi1dkk84qs31bx:recovery_setup [division,   peid,onesol_pe_names_division,onesol_pe_names_peid]))必须具有相同的功能   列数作为引用的主键(ONESOL_pe_names   [分割,pe_id])

我将oneSolName列设置为class RecoverySetup implements Serializable { static mapping = { table "recovery_setup" id composite: ["division", "peid", "orgkey"] columns { division column: 'division', length: 10, sqlType: "char" peid column: 'peid', length: 12, sqlType: "char" orgkey column: 'org_key', length: 8, sqlType: "char" oneSolName column: ['division', 'peid'] } } static belongsTo = [oneSolName: OnesolPeNames] .... ['division', 'peid']来自何处?

难道我的复合键是PK的一部分,但也扮演FK的角色,被称为“派生身份”?

1 个答案:

答案 0 :(得分:3)

我有类似的问题,我以这种方式解决了映射表:

class RecoverySetup implements 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]
    ...
}