如何使用预测和标准获得不同的结果
我正在尝试使用 Grails 中的条件加载不同的父级。查询如下
查询:
def criteria = Parent.createCriteria();
results = criteria.list(max:params.max, offset:params.offset){
projections{ groupProperty('id') }
children{
books{
like('title',"%book")
}
}
order("id","asc")
}
域类
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 = {
}
}
问题:我无法获取不同的父行。
其他采用的方法及其结果:我不知道为什么groupProperty不起作用。我在投影中尝试了“distinct”而不是“groupProperty”,但它也没有取得成果!如果我使用criteria.listDistinct而不是criteria.list,那么我可以获得不同的父行,但早期的方法需要从额外的totalCount中获取查询分页。因此,我非常有兴趣使用 criteria.list 获取不同的父行
提前致谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您更改条件查询以包含不同的根实体结果转换器,则可以获得与
criteria.listDistinct
相同的效果,如下所示:然而,grails 不为 listDistinct 调用返回分页结果是有原因的因此可能需要使用
in
运算符进行 HQL 查询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: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