Grails createCriteria父级带有最后一个子级

问题描述 投票:0回答:1

我如何只将lastChild和parentObject一起阅读

Parent {
  hasMany[children: Child]
}

这是我的代码

Parent.withCriteria() {
        eq("active", true)
        children {
            order("dateCreated", "desc")
            maxResults(1)
        }
    }

但不起作用。我如何阅读最近更新的孩子的父母

grails gorm createcriteria
1个回答
0
投票

GORM / Hibernate中的[hasMany没有过滤语义,并且始终返回所有引用的对象。

在简单的情况下,您必须引发2个查询:查找所有Parents和查找所有最近更新的子代。

[另一种选择是颠倒搜索逻辑:首先寻找孩子,将他们按lastUpdated分组,然后拉他们的父母。当然,您必须从子代到父代都有反向引用:

class Child {
  static belongsTo = [ parent:Parent ]
}

然后查询可能类似于:

Child.withCriteria{
  projections{
    groupProperty 'id'
    max 'lastUpdated'
  }
  parent{
    eq "active", true
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.