如何使用Projections和Criteria获得不同的结果 [英] how to get distinct results using Projections and Criteria

查看:142
本文介绍了如何使用Projections和Criteria获得不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Grails中使用Criteria加载不同的父母。查询如下:

查询:

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

$ b

域类

  class Parent {

字符串名称

static hasMany = [children:Child]

static constraints = {
}
}


class子元素{

字符串名称
父级父级

static belongsTo = [parent: Parent]
static hasMany = [books:Book]
static constraints = {
}
}


class Book {

字符串标题
孩子孩子

static belongsTo = [child:Child]
静态约束nts = {
}
}

问题 :我无法获得不同的父行。



其他采用的方法及其结果:我不知道为什么 groupProperty 不起作用。我已经在投影中尝试过 distinct ,而不是 ,它也不是果实!如果我使用 criteria.listDistinct 而不是 criteria.list ,那么我可以获得不同的父行,但更早的方法需要额外获取 totalCount 查询分页。因此,我很高兴在使用 criteria.list

获得不同的父行。

预先感谢

criteria.listDistinct 相同的效果像这样的变换器:

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

然而,grails不返回pageDistinct调用的分页结果,因此可能需要在运算符
中使用进行HQL查询

I am trying to load distinct Parents using Criteria in Grails. The query is as following

Query:

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 am unable to get distinct Parent Rows.

Other adopted Approaches and their results: I donot know why groupProperty is not working. I have tried distinct in projections instead of groupProperty and it isnt fruitfull too!. if i use criteria.listDistinct instead of criteria.list then i am able to get distinct Parent Rows but earlier approach require to get totalCount from extra query for pagination. Therefore i am highly intersted in getting distinct Parent Rows using criteria.list

Thanks in advance

解决方案

You can achieve the same effect as with criteria.listDistinct if you change the criteria query to include distinct root entity results transformer like this:

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

There is however a reason why grails does not return paged results for the listDistinct call so it might be a case to resort to an HQL query with the in operator

这篇关于如何使用Projections和Criteria获得不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆