grails joinTable 与旧数据库

发布于 2024-10-26 22:06:54 字数 1061 浏览 4 评论 0原文

我有一个一对多的表映射,我想用 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 映射器能够将事物很好地连接在一起。

I have a one-to-many table mapping that I want to do with grails. I have a legacy database where I can't change the table structure. I have everything set up, but the only thing I can't figure out is how to get grails to notice the existing foreign keys instead of creating it's own column. I have something like this (simplified):

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"
     }
}

As a side note, I see that joinTable in the grails document says "column" is the inverse column, and "key" is the foreign key. The examples in the docs aren't helpful. I have "Grails in Action" and it says it's going to provide a one-to-many example, then shows a many to many example, and either way it doesn't seem to be a complete example (half the model is missing)!

Any ideas? I have this legacy database, and I'm going to use it read-only. I just want the O/R mapper to hook things together nicely.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

゛时过境迁 2024-11-02 22:06:54

Customer 域对象中的 joinTable 应该使用列,而不是键:

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

另外,名称: 应该是联接表的名称,而不是注释表的名称。

Your joinTable in the Customer domain object should use column, not key:

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

Also, the name: should be the name of the join table, not the name of the Note table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文