我有一个类叫做School它有很多,所以现在当我阅读usingStudents的实例时,我不希望所有的学生都被急切地获取,所以我将获取策略更改为,SchoolSchool.read(id)lazy
但现在它会获取students我访问的所有时间school.students,我想先手动设置 5 students,然后如果需要 5-10,依此类推
我们如何以这种方式自定义延迟获取?
School有很多Student
StudentSchool与个人无关
我有一个类叫做School它有很多,所以现在当我阅读usingStudents的实例时,我不希望所有的学生都被急切地获取,所以我将获取策略更改为,SchoolSchool.read(id)lazy
但现在它会获取students我访问的所有时间school.students,我想先手动设置 5 students,然后如果需要 5-10,依此类推
我们如何以这种方式自定义延迟获取?
School有很多Student
StudentSchool与个人无关
您可以使用batchSize自定义延迟加载期间获取的结果数量:
class Book {
…
static mapping = {
batchSize 10
}
}
请参阅Grails 文档。
编辑
School.students您可以使用查询创建一个简单的服务,而不是调用
class SchoolService{
def getLastStudents(School school, int max, int offset){
// (Not tested but should be something like this)
def query = "select student from School school join school.students student where school=:school"
def students = School.executeQuery(query, [school: school], [max: max, offset: offset]) }
}
然后调用schoolService.getLastStudents(school, 10, 0)例如获取最后 10 名学生。
您可以在官方文档中阅读有关 Gorm 标准的所有信息。