Grails GORM 域类关系
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)
}
}
我想加载一本带有出版商和作者值的书。 当我收到带有查询的书籍时:
def book2 = Book.findAllByAuthor(author)
我得到与作者相关联的响应,但出版商在另一个查询中只有 id 和名称类:
def book3 = Book.findAllByPublisher(publisher)
我检索到相反的结果,我有带有出版商数据但作者的书只有id和类名。
定义的模型错误在哪里? o 查询方式有错误?
编辑:
我需要一种仅使用如下查询来检索值的方法:
def book2 = Book.findAllByAuthor(author, [fetch:[publisher:'eager']])
在这个查询中,我可以管理发布者的值。
问题:如果出版商有一个 hasmany
或 Domain
相关内容,那么我可以读取该书的属性吗?
谢谢。 谢谢。
Grails 1.1.1
Goovy 1.5.7
In a relationship such this:
Author 1 -- n Book n -- 1 Publisher
Defined in 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)
}
}
I want to load a Book with the values of Publisher and Author.
When i get a Book with the query:
def book2 = Book.findAllByAuthor(author)
I get the response with the autor assosiated but the publisher only have the id and name class in the other query:
def book3 = Book.findAllByPublisher(publisher)
I retrieve me the inverse result,i have the book with the publisher data but the author only have the id and the class name.
Where is the error in the defined model ? o there is an error in the way to do the queries ?
Edit:
I need the way to retrieve the values only with the query like this:
def book2 = Book.findAllByAuthor(author, [fetch:[publisher:'eager']])
In this one I can manage the value of publisher.
Question: If publisher had a hasmany
or Domain
related, getting the book I'm able to read the attributes?
Thanks.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
默认情况下,延迟获取与 gorm 关联一起使用。如果要启用急切获取,您可以通过将以下映射块添加到您的 Author 域类来修改 ORM DSL:
或者您可以通过在定义图书关系后添加以下代码来更改域对象中的获取模式。
对您的发布者域对象执行相同的操作应该可以让您完成您想要的任务。您确实要小心可能会加载比预期更多的数据的后果。
Lazy fetching is used by default with gorm associations. If you want to enable eager fetching, you can modify the ORM DSL by adding the following mappings block to your Author domain class:
or you could change the fetch mode in the domain object by adding following code after your books relationship is defined.
Doing the same to your Publisher domain object should allow you to accomplish what you want. You do want to be careful of the consequence that you may load more data than you intend to.
get() 方法不应该返回您正在寻找的内容吗?
示例: def book2 = Book.get(author)
Shouldn't the get() method return what you are looking for?
Example: def book2 = Book.get(author)
您最好使用 Criteria 并明确定义应立即加载哪些关系。只需在查询中提及关系即可。
例子:
You'd better use Criteria and explicitly define which relations should be loaded eagerly. Just mention relation in the query.
Example: