【问题标题】:Grails custom names for composite key in many to many relationship in joinTable (legacy db)joinTable 中多对多关系中复合键的 Grails 自定义名称(遗留数据库)
【发布时间】:2014-08-29 14:52:50
【问题描述】:

如果一个域类具有复合 id,grails/gorm 似乎会在多对多关系上忽略连接表上的列名,例如:

class Following implements Serializable {
    ...
    static hasMany = [operationTypes:OperationType]
    static belongsTo = OperationType
    static mappedBy = [operationTypes:'followings']

    static mapping = {
        ...
        id composite: ['offer', 'user']
        offer column: 'offer_oid'
        user column: 'user_oid'
        operationTypes joinTable: [name: 'operationtype_following', key: ['favorite_user_offer_oid', 'favorite_user_user_oid']]
    }
}

和:

class OperationType implements Serializable {
    ...
    static hasMany = [offers:Offer, advices:Advice, followings:Following]
    static mappedBy = [followings:'operationTypes']

    static mapping = {
        ....
        followings joinTable: [name: 'operationtype_following', key: 'operationtype_oid']
    }
}

结果: MappingException: 外键 (FK_lhri681gwbef5a9y6ylhoakpj:operationtype_following [favorite_user_offer_oid, favorite_user_user_oid,Following_offer_id,Following_user_id])) 的列数必须与引用的主键 (favorite_user [user_oid,offer_oid]) 相同

那么为什么它不真正忽略列名,而是将生成的列名添加到指定的列名?

使用 Grails 2.4.3。任何帮助表示赞赏

【问题讨论】:

  • 我们遇到了完全相同的问题 - 您解决了吗?
  • 不,我现在必须将正常的自动增量 ID 放入该表。只是在 grails 应用程序的关系中使用...
  • 好的,所以您从“Following”域中删除了复合键?我们计划将复合键保留为主标识符,并自行管理多对多关系。
  • 是的,我将普通整数设置为 id 并将(前​​)复合列设置为外键...找不到保留复合键的解决方案 :( 但至少旧代码/应用程序继续使用这个数据库。

标签: grails many-to-many grails-orm composite jointable


【解决方案1】:

我能够在 m:m 关系中使用复合外键。这使用以下映射语法。 SITE 和 MACHINE_FK 是连接表的属性。

static mapping = {
        mstAttributeList {
            column name: 'SITE'
            column name: 'MACHINE_FK', sqlType: 'nvarchar2'             
        }           
        mstAttributeList joinTable: [name: 'MST_MACHINE_ATTRIBUTE', key: ['MACHINE_FK', 'SITE']]
}

static hasMany = [ mstAttributeList : com.gknsintermetals.mda.master.MstAttribute ]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-10
    • 2017-05-07
    • 2012-06-03
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    相关资源
    最近更新 更多