作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
例如,我有一个父类Author:
class Author {
String name
static hasMany = [
fiction: Book,
nonFiction: Book
]
}
class Book {
String title
static belongsTo = [author: Author]
}
def fictBook = new Book(title: "IT")
def nonFictBook = new Book(title: "On Writing: A Memoir of the Craft")
def a = new Author(name: "Stephen King")
.addToFiction(fictBook)
.addToNonFiction(nonFictBook)
.save()
最佳答案
我认为,这不是建模数据的最佳方法。我会这样做。
class Author {
String name
static hasMany = [books: Book]
}
class Book {
String title
BookTypes bookType
static belongsTo = [author: Author]
}
enum BookTypes {
FICTION,
NON_FICTION
}
def author = Author.get(1)
def nonFictionByAuthor = Book.findAllByAuthorAndBookType(author, BookTypes.NON_FICTION)
def author = Author.get(1)
def fictionBooks = author.books.findAll { it.bookType == BookTypes.FICTION }
def fictionBook = Book.findByTitleAndBookType('Title001', BookTypes.FICTION)
关于grails - Cereal ,哥伦。按 parent 找 child ,按 child 找 parent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29707279/
我是一名优秀的程序员,十分优秀!