hibernate - Grails 域关系

标签 hibernate grails pagination lazy-loading grails-orm

我有一个名为 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/

相关文章:

java - 如何防止@Cascade({CascadeType.ALL}) 保存重复的重复记录

php - 调用未定义的方法 Illuminate\Database\Query\Builder::links()

mysql - 使用分页和分数在两个不同的表中搜索数据

grails - 此M:M模式的名称

javascript - 使用 onblur 来相乘 <g :field> grails

grails - Grails无方向hasMany createCriteria

ruby-on-rails - Kaminari:数组的未定义方法 `total_pages'

mysql - Hibernate - 在表的所有列中搜索文本

hibernate - 没有外键的 JPA 关联

java - 带接口(interface)的 Hibernate HQL