grails joinTable与旧数据库

时间:2011-03-26 00:26:16

标签: hibernate grails gorm

我有一个一对多的表映射,我想用grails做。我有一个遗留数据库,我无法更改表结构。我已经设置了所有内容,但我唯一想知道的是如何让grails注意到现有的外键而不是创建它自己的列。我有类似的东西(简化):

class Customer {
    String listID
    String name
    String address
    // more fields etc.

    static hasMany [notes : Note]

    static mapping = {
        table name:"customers"
        id name:"listID",generator:"assigned"

        // doesn't work, creates a foreign key column in customer_notes table
        // with key: customer_id. I want it to just use the existing column
        // CustomerListID, which has the correct foreign key
        notes joinTable:[name:"customer_notes",key:"CustomerListID"]            
    }
}

class Note {
    String noteID
    String customerListID

    static mapping = {
        table name:"customer_notes"
        id name:"NoteID",generator:"assigned"
     }
}

作为旁注,我看到grails文档中的joinTable说“column”是反向列,“key”是外键。文档中的示例没有帮助。我有“Grails in Action”,它说它将提供一对多的例子,然后显示多对多的例子,无论哪种方式,它似乎都不是一个完整的例子(缺少一半的模型) !

有什么想法吗?我有这个遗留数据库,我将以只读方式使用它。我只是希望O / R映射器可以很好地连接在一起。

1 个答案:

答案 0 :(得分:0)

Customer域对象中的joinTable应使用column,而不是key:

notes joinTable:[name:"CustomerNotes",column:"CustomerListID"]

此外,名称:应该是连接表的名称,而不是Note表的名称。

相关问题