我是 Grails 和 GORM 的新手,我尝试实现“一对多”关系。
我尝试了文档中的示例:
class Book {
String title
}
class Author {
static hasMany = [books: Book]
String name
}
以下是生成的表:
AUTHOR
- Id (PK)
- Name
BOOK
- Id (PK)
- Title
AUTHOR_BOOK
- Author_Books_Id
- Book_Id
我期待更像:
AUTHOR
- Id (PK)
- Name
BOOK
- Author_Id (PK)
- Book_Index (PK)
- Title
有没有办法实现这一点(摆脱连接表)?
最佳答案
您应该声明 Book 属于 Author。使用belongsTo,您声明Book 表中有一个外键,该外键保持对Author 的id 列的引用。像这样:
class Book {
String title
static belongsTo = [author: Author]
}
class Author {
static hasMany = [books: Book]
String name
}
关于Grails "One to Many"没有表连接的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10637523/