2012-12-06 71 views
0

我有一箇舊的數據庫,我不能改變的,我有這樣的設置如何避免ImprovedNamingStrategy在joinTable Grails中

class Foo { 

    static hasMany = [bars:Bar] 

    static mapping = { 
    version false 

    columns { 
     id column: "FooId" 
     color column: "FooColor" 

     bars joinTable: [name: "FooBar", key: 'FooId', column: 'BarId'] 
    } 

    transient 
    def getBarName(){ 
     ((Bar)this.bars.toArray()[0]).name 
    } 
} 

class Bar { 

    static hasMany = [foos:Foo] 

    static belongsTo = [Foo, Baz] 

    static mapping = { 
    version false 

    columns { 
     id column: "BarId" 
     name column: "BarName" 
    } 
} 

當我嘗試在控制器休眠訪問方法getBarName()的翻譯反向列名稱爲「bar_id」。有沒有辦法像id和property欄一樣設置映射?

並在旁註。我如何正確實現getBarName()? Thacan't可能是正確的實現......

* 編輯 *
------------------------- ---------------------------------------------
顯然我上面還不清楚。問題是,我已經有了它的形式,因爲我想避免的joinTable域對象

------------------- 
|RowId|FooId|BarId| 
------------------- 
| 1 | abc | 123 | 
------------------- 

Benoit的答案是不是真的適用於這種情況下,連接列。

* EDIT 2 *
----------------------------------- -----------------------------------
解決了它。雖然不明白...但拆分兩個域類之間的連接表信息和它的作品...

class Foo { 

    static hasMany = [bars:Bar] 

    static mapping = { 
    version false 

    columns { 
     id column: "FooId" 
     color column: "FooColor" 

     bars joinTable: [name: "FooBar", key: 'FooId'] 
    } 

    transient 
    def getBarName(){ 
     ((Bar)this.bars.toArray()[0]).name 
    } 
} 

class Bar { 

static hasMany = [foos:Foo] 

    static belongsTo = [Foo, Baz] 

    static mapping = { 
    version false 

    columns { 
     id column: "BarId" 
     name column: "BarName" 

     bars joinTable: [name: "FooBar", key: 'BarId'] 
    } 
} 

回答

0

如文檔Many-to-One/One-to-One Mappings and One-to-Many Mapping中指出:

隨着雙向one-to - 您可以按照上一節中關於一對一關聯的示例,通過更改關聯 多方的列名來更改使用的外鍵列 。 但是,對於單向關聯,外鍵需要在關聯本身上指定的 。

泰豐給出的例子是:

class Person { 
String firstName 
static hasMany = [addresses: Address] 
static mapping = { 
     table 'people' 
     firstName column: 'First_Name' 
     addresses column: 'Person_Address_Id' 
    } 
} 
+0

這不回答這個問題。請參閱編輯進行澄清 – Spade