2014-08-29 39 views
2

Grails的/格姆似乎忽略許多連接表一對多的關係,如果一個域類有複合的ID,例如列名:爲多對多的關係在joinTable(傳統DB)複合鍵的Grails自定義名稱

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]))必須具有相同的列數作爲參照的文ced主鍵(favorite_user [user_oid,offer_oid])

那麼爲什麼它不真的忽略列名稱,而是將生成的列名添加到指定的列名?

使用Grails 2.4.3。任何幫助讚賞

+0

我們有一個完全相同的問題 - 你是否知道這個問題? – RRK 2014-11-13 17:25:43

+0

不,我現在必須將正常的自動增量編號放到該表格中。只是在grails應用程序的關係中使用... – denis111 2014-11-17 11:33:43

+0

好了,所以您從「以下」域中刪除了組合鍵?我們計劃將組合鍵保留爲主要標識符,並自行管理多對多關係。 – RRK 2014-11-18 17:10:42

回答

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 ]