Grails GORM - return 数组对象的计数 属性 > 0

Grails GORM - return count of objects who's array property > 0

考虑以下因素;

class Person {
    int id
    String name

    static hasMany = [cars : Car]
}

class Car {
   int id
   String brand

   static belongsTo = Person
   static hasMany = [owners: Person]
}

以上将导致 person_cars 加入 table。我想知道的是 table 中是否有任何条目,换句话说;

目前是否存在拥有汽车的人。

乐于使用任何可用的机制(finders/criteria/HQL 等)

我的意见是添加PersonCar实体更好。

针对您的问题:

Car.count() > 0

因为car belongTo Person,所以不能是没有添加到person的car的任何副本。 如果 person 可以为空,您可以使用:

Car.countByPersonIsNotNull()

认为如果车里有人,那里面就有了价值table。

知道了!比我想象的简单很多。

 Person.createCriteria().count {
   owners {
     count
   }
 }

这有效地给出了 person_cars 条记录的数量。