grails joinTable 与旧数据库
我有一个一对多的表映射,我想用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Customer 域对象中的 joinTable 应该使用列,而不是键:
另外,名称: 应该是联接表的名称,而不是注释表的名称。
Your joinTable in the Customer domain object should use column, not key:
Also, the name: should be the name of the join table, not the name of the Note table.