简体   繁体   中英

grails domain relationship

I have a class called School it has many Students , so now when I read instance of School using School.read(id) , I don't want all the students to be fetched eagerly so I changed fetching strategy to lazy ,

but now it will fetch all the students when I will access school.students , I want to set manually first 5 students then if require 5-10, so on

How can we customize lazy fetching this way ?

School has many Student

Student has no relationship with School individually

You can customize how many results are fetching during lazy loading using batchSize :

class Book {
…
static mapping = {
    batchSize 10
   }
}

See the Grails documentation .

Edit

Instead of calling School.students you can create a simple service with your query

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]) }

}

Then call schoolService.getLastStudents(school, 10, 0) for example to get the last 10 students.

You can read all about Gorm criteria in the official documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM