Yan*_*ann 3 grails grails-orm grails-domain-class
我是Grails和GORM的新手,我尝试实现"一对多"关系.我在doc中尝试了这个例子:
class Book {
String title
}
class Author {
static hasMany = [books: Book]
String name
}
Run Code Online (Sandbox Code Playgroud)
以下是生成的表:
AUTHOR
- Id (PK)
- Name
BOOK
- Id (PK)
- Title
AUTHOR_BOOK
- Author_Books_Id
- Book_Id
Run Code Online (Sandbox Code Playgroud)
我期待的更像是:
AUTHOR
- Id (PK)
- Name
BOOK
- Author_Id (PK)
- Book_Index (PK)
- Title
Run Code Online (Sandbox Code Playgroud)
有没有办法实现这一点(摆脱连接表)?
Tom*_*ski 17
您应该声明该书属于作者.使用belongsTo,您声明Book表中有外键,该外键保持对Author的id列的引用.像这样:
class Book {
String title
static belongsTo = [author: Author]
}
class Author {
static hasMany = [books: Book]
String name
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2676 次 |
| 最近记录: |