GORM 一对多关系——创建 3 个表而不是 2 个
我的所有
问题是我正在 GORM 中创建一对多关系,并期望创建 2 个数据库表作为支持对象。创建了 3 个,这使得 SQL 查询过于复杂。
我在 GORM 文档中创建了一对多的紧密变体:
class Status {
List errorMessage
static hasMany = [errorMessage:ErrorMessage]
}
以及错误消息类:
class ErrorMessage {
String message
static belongsTo = Status
}
我希望这会创建两个数据库表:
CREATE TABLE status {
ID NUMBER(19,0),
VERSION NUMBER(19,0),
//other fields
}
CREATE TABLE error_message {
ID NUMBER(19,0),
VERSION NUMBER(19,0),
STATUS_ID NUMBER(19,0),
MESSAGE VARCHAR(255)
//other fields
}
但实际上它需要第三个表,
CREATE TABLE status_text {
status_text_id NUMBER(19,0),
text_idx NUMBER(19,0),
text_id NUMBER(19,0)
}
将状态添加到 ErrorMessage (一个 hack因为我不希望 ErrorMessage 引用 Status) 类删除第三个表,但保留第二个外键,导致 Text 子对象具有两个外键字段。
我想要的很简单 - 只要附加到父对象的一组对象就会被删除 - 有什么想法我做错了吗?
谢谢
All
My problem is I'm creating a 1-to-many relationship in GORM and expect 2 database tables to be created as backing objects. 3 are created which makes SQL queries overly complex.
I've created a close variant on the 1-to-many in the GORM documentation:
class Status {
List errorMessage
static hasMany = [errorMessage:ErrorMessage]
}
and the error message class:
class ErrorMessage {
String message
static belongsTo = Status
}
I expected this to create two database tables:
CREATE TABLE status {
ID NUMBER(19,0),
VERSION NUMBER(19,0),
//other fields
}
CREATE TABLE error_message {
ID NUMBER(19,0),
VERSION NUMBER(19,0),
STATUS_ID NUMBER(19,0),
MESSAGE VARCHAR(255)
//other fields
}
but actually it wants a third table,
CREATE TABLE status_text {
status_text_id NUMBER(19,0),
text_idx NUMBER(19,0),
text_id NUMBER(19,0)
}
Adding Status to the ErrorMessage (a hack as I don't want ErrorMessage to have a reference to Status) class removes the third table but keeps the second foreign key resulting in the Text child object having two foreign key fields.
What I want is simple - just a set of objects attached to the parent will be deleted when it is - any thoughts what I'm doing wrong?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您不能满足这两个要求,即您有级联删除并且没有连接表。您需要
belongsTo
来获取级联删除,这使得关系是双向的。要删除连接表,请使用Map语法命名belongsTo:I don't think you can satisfy both requirements, i.e. that you have cascading deletes and no join table. You need the
belongsTo
to get cascading deletes and that makes the relationship bidirectional. To remove the join table, name the belongsTo with the Map syntax: