Grails标准预测 - 获取行数 [英] Grails criteria projections - get rows count

查看:84
本文介绍了Grails标准预测 - 获取行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Hotel实体:

  class Hotel {
City city
}

现在,我需要计算给定城市的酒店数量。
可以这样做:

  def hotels = Hotel.findAllByCity(city)
def cnt = hotels.size()

但它非常脏。它似乎与标准,它会更好,但我不知道如何实现它...

解决方案

有动态计数器以及域对象上的查找器:

  Hotel.countByCity(city)

更多详情请参阅用户指南,当然

I have Hotel entity:

class Hotel {
City city
}

Now, i need count of hotels with given city. It could be done in this way:

def hotels = Hotel.findAllByCity(city)
def cnt = hotels.size()

But it's very dirty way. It seems that with criteria it would be better, but i have no idea how to implement it...

解决方案

There are dynamic counters as well as finders on domain objects:

Hotel.countByCity(city)

More details in the user guide, of course

这篇关于Grails标准预测 - 获取行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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