【发布时间】:2010-12-22 18:46:03
【问题描述】:
Grails 1.1.1 Goovy 1.5.7
在这样的关系中:
作者 1 -- n 书 n -- 1 出版商
在 Grails 中定义:
class Author {
String firstName
String lastName
static hasMany = [books: Book]
static constraints = {
books(nullable: true)
}
}
class Book {
String title
Author author
Publisher publisher
static constraints = {
author(nullable: true)
publisher(nullable: true)
}
}
class Publisher {
String name
static hasMany = [books: Book]
static constraints = {
books(nullable: true)
}
}
我想加载具有 Publisher 和 Author 值的 Book。 当我得到一本带有查询的书时:
def book2 = Book.findAllByAuthor(author)
我得到了与作者关联的响应,但发布者在另一个查询中只有 id 和 name 类:
def book3 = Book.findAllByPublisher(publisher)
我检索到相反的结果,我有带有出版商数据的书,但作者只有 id 和类名。
定义的模型中的错误在哪里? o 查询方式有误?
编辑:
我需要这样的查询来检索值的方法:
def book2 = Book.findAllByAuthor(author, [fetch:[publisher:'eager']])
在这个我可以管理出版商的价值。
问题:如果出版商有一个hasmany 或Domain 相关,得到我能够阅读的属性的书?
谢谢。 谢谢。
【问题讨论】:
-
Grails 1.1.1 捆绑了 Groovy 1.6.3 ($GRAILS_HOME/lib/groovy-all-1.6.3.jar),除非您替换了那个 jar,否则 Grails 1.1.1 将使用 Groovy 1.6。 3.没什么大不了的,但值得评论:-)
标签: grails dns grails-orm eager-loading eager