如何不使用Grails从数据库加载多个实体
我有三门课:如何不使用Grails从数据库加载多个实体,grails,has-many,Grails,Has Many,我有三门课: class Author { static hasMany = [books: Book] static belongsTo = [company: Company] String name } class Book { static mapping = { collection "documents" id generator: 'assigned',index: true, indexAttributes:[b
class Author {
static hasMany = [books: Book]
static belongsTo = [company: Company]
String name
}
class Book {
static mapping = {
collection "documents"
id generator: 'assigned',index: true, indexAttributes:[background:true, unique:true, dropDups:true]
}
String id
String name
}
class Company {
static mapping = {
collection "documents"
id generator: 'assigned',index: true, indexAttributes:[background:true, unique:true, dropDups:true]
}
String id
String name
}
我想用
Author author = Author.getByCompanyAndBook(1,1);
但是,当运行此Grails时,它会从数据库中检索所有Book对象
我只需要这些书作为作者的标识符,我不打算使用这些对象
我有没有办法强迫Grails不要从数据库中获取书籍和公司
我尝试使用:
static mapping = {
books lazy: true
}
但所有的书和公司都装上了
编辑:
我正在使用mongo db作为我的数据库。这很奇怪-默认情况下,集合是惰性的,因此在访问books属性之前,获取作者不应该检索books集合中的任何内容。但无论如何,即使它按预期工作,我建议您避免使用映射集合。请查看下面的内容,其中显示了为什么集合不必要地昂贵,并提供了将成本降至最低的变通方法。感谢您的回复,实际上我正在使用Intellij调试器来找出得到的回报。我使用的是mongodb,所以我无法(至少我知道)检查确切的查询是什么。但是在调试器中,我可以访问Books集合中的所有Books数据。您使用的是什么grails版本?