How to get great results with predictions and criteria

I am trying to load various parents using criteria in Grails. The request is as follows

Request:

def criteria = Parent.createCriteria(); results = criteria.list(max:params.max, offset:params.offset){ projections{ groupProperty('id') } children{ books{ like('title',"%book") } } order("id","asc") } 

Domain Classes

  class Parent { String name static hasMany = [children:Child] static constraints = { } } class Child { String name Parent parent static belongsTo = [parent:Parent] static hasMany = [books:Book] static constraints = { } } class Book { String title Child child static belongsTo = [child:Child] static constraints = { } } 

Question I cannot get individual parent rows.

Other accepted Approaches and their results: I do not know why groupProperty does not work. I tried great projections instead of groupProperty, and that is also not very useful! if I use the .listDistinct criteria instead of the .list criteria, then I can get different Parent lines, but for an earlier approach, I need to get totalCount from an additional request for pagination. Therefore, I am very interested in getting various parent strings using .list criteria

Thanks in advance

+7
source share
1 answer

You can achieve the same effect as with criteria.listDistinct if you modify the criteria query to include a separate transformer for the results of root entities:

  results = criteria.list(max:params.max, offset:params.offset){ children{ books{ like('title',"%book") } } resultTransformer Criteria.DISTINCT_ROOT_ENTITY order("id","asc") } 

However, there is a reason that grails does not return paginated results for calling listDistinct, so you might have a chance to access a HQL query with the in operator

+3
source

All Articles