user1298426
user1298426

Reputation: 3717

PagedResultList issue in grails 2

I was using 1.3.7 before and constructor for PagedResultList in that PagedResultList constructor was accepting list. So my code was

def result = [...]

def pagedResult = new PagedResultList(result)

now the constructor is changed as

PagedResultList(GrailsHibernateTemplate template, Criteria crit)

Can you please help me convert result list to PagedResultList in 2.x ?

Upvotes: 1

Views: 774

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122414

PagedResultList appears to have been changed in Grails 2 to calculate the totalCount lazily on first access rather than the count having to be calculated up-front when it might not ultimately be required. But it's 4 lines of Groovy to write your own drop-in replacement, thanks to the Delegate AST transformation:

class MyPagedResultList {
  @Delegate List theList
  int totalCount
}

// create one using new MyPagedResultList(theList:result, totalCount:total)

What the transformation does is automatically add all the methods of the delegate type (in this case List) to the target type (in this case MyPagedResultList), implemented by delegating to the delegate object. So it makes MyPagedResultList implement the List interface without having to write out all the relevant methods by hand.

Upvotes: 4

Related Questions