我有一个名为 School
的类,它有许多 Student
,所以现在当我使用 School.read 读取
,我不希望所有学生都被急切地获取,所以我将获取策略更改为School
的实例时(id)lazy
,
但现在当我访问 school.students
时,它将获取所有 students
,我想手动设置前 5 个 students
然后如果需要5-10,依此类推
我们如何以这种方式自定义延迟获取?
学校
有很多学生
学生
与学校
个人没有任何关系
最佳答案
您可以使用batchSize自定义延迟加载期间获取的结果数量:
class Book {
…
static mapping = {
batchSize 10
}
}
请参阅Grails documentation .
编辑
您可以使用查询创建一个简单的服务,而不是调用 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 名学生。
您可以在 official documentation 中阅读有关 Gorm 标准的所有内容。 .
关于hibernate - Grails 域关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13597037/