title: Correct pagination withCriteria while looking at many-to-one table (Grails)
body: <p>There is a paginated search for <code>Product</code> table. The search looks at <code>Product</code> objects and also at <code>ProductParam</code> objects.</p>
  
  <p>The problem is there are duplicate Products in search result: having 25 items per page, two thirds are duplicated Products.</p>
  
  <p>If applying <a href="https://stackoverflow.com/questions/7181470/problem-with-duplicates-using-grails-withcriteria-function-with-a-many-to-many">resultTransformer org.hibernate.Criteria.DISTINCT_ROOT_ENTITY</a> in criteria builder, the resulted items per page are fewer than 25 - it's about 6.</p>
  
  <p>In both cases, the search is broken. Could that issue be solved? (Without rewriting the code completely.)</p>
  
  <pre><code>class Product {
    String name
    static hasMany = [ params: ProductParam ]
  }
  
  class ProductParam {
    String key
    String value
    static belongsTo = [ product: Product ]
  }
  
  HibernateCriteriaBuilder criteriaBuilder = Product.createCriteria()
  PagedResultList results = criteriaBuilder.list(max: 25, offset: offset) {
    or {
      // searching in Product
      ilike 'name', "%${query}%"
  
      // searching in ProductParam
      createAlias('params', 'pp')
      ilike 'pp.value', "%${query}%"
    }
    //resultTransformer org.hibernate.Criteria.DISTINCT_ROOT_ENTITY
  }
  </code></pre>
  
  <p>Grails 2.2.0, Postgres</p>