Grails - 查询域实例的属性'性能

时间:2014-11-23 16:09:25

标签: grails

到目前为止,我遇到的所有查询示例都面向域类,例如:

Account.where, Account.withCriteria, Account.findxxxx但是如果我想查询实例的属性'特性?例如,如果我c公司实例具有部门d并且我希望获得该公司实例的list of all the departments具有12名员工(部门财产)或更少?这种查询的代码是什么?

类似的东西:

c.findAllD's(such that d.numberOfEmployees <= 12)

另外,有人能指出这些基于实例的查询的文献吗?我还没有碰到它。

2 个答案:

答案 0 :(得分:1)

看起来您想使用Named Queries

答案 1 :(得分:1)

最简单的方法是使关联成为双向的,即

class Company {
  static hasMany = [departments:Department]
}

class Department {
  Company company
  int numberOfEmployees

  static belongsTo = [company:Company]
}

然后,您只需从Department端开始查询,例如

def c = Company.get(...) // or however you obtain your Company instance
def departments = Department.findAllByCompanyAndNumberOfEmployeesLessThanEquals(c, 12)