2013-05-10 40 views
0

這是一個語法問題。我想富之間的一個一對多的關係 - >酒吧(這裏簡化):Grails中的一對多複合鍵和不同列名稱

class Foo { 
    String fooPK1, fooPK2 

    static mapping = { 
     id composite: ["fooPK1", "fooPK2"] 
    } 

    static hasMany = [bars: Bar] 
} 

class Bar { 
    String fooPK1, dumbNameForFooPK2, barPK1, barPK2 
    Foo myFoo 

    static mapping = { 
     id composite: ["barPK1", "barPK2"] 
     columns { 
      myFoo[:] { 
       column name: "FOO_PK_1" 
       column name: "?????????????" 
      } 
     } 
    } 
} 

在這種情況下,顯然Foo.fooPK1映射到Bar.fooPK1,但我需要Foo.fooPK2映射到酒吧.dumbNameForFooPK2。希望這是有道理的。

我的問題是我不知道語法應該是什麼(或者如果有更好的方法來做到這一點!),從我能找到的,grails文檔沒有真正的幫助。

回答

1

你需要重命名bar中的外鍵列聲明,賴特?

class Bar { 
    Foo myFoo 

    static mapping = { 
    columns { 
     myFoo { 
     //declare them in the order of your id composite. 
     column name: "foo_pk_1" 
     column name: "dumb_name_for_foo_pk_2" 
     } 
    } 
    } 

} 
+0

好的,我明白了。所以Foo.pk [0] == Bar.column [0],Foo.pk [1] == Bar.column [1],不管名字是什麼? – 2013-05-10 17:40:35

+1

是的,但只是在myFoo {}塊內。 – 2013-05-10 18:05:54

相關問題