Grails标准投影 - 获取行数

时间:2010-02-24 16:46:10

标签: java hibernate grails

我有酒店实体:

class Hotel {
City city
}

现在,我需要有特定城市的酒店数量。 它可以这样做:

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

但它非常方式。似乎有了标准会更好,但我不知道如何实现它......

2 个答案:

答案 0 :(得分:46)

Dave is right您可以使用countBy*方法进行简单计数。如果您需要两个以上的标准,则必须恢复为criteria apiHQL或SQL。通常需要两个以上的标准,特别是在活跃且不断发展的代码库中。

以下是如何使用Criteria api执行projections的示例:

def c = Hotel.createCriteria()

def hotelCount = c.get {
    projections {
        count('id')
    }
    gt("stars", 2)          
    eq("city", city)            
    eq("deleted", false)

}

或者(更优雅地)你甚至可以像下面这样使用Criteria#计数:

def c = Hotel.createCriteria()

def hotelCount = c.count {
    gt("stars", 2)          
    eq("city", city)            
    eq("deleted", false)

}

为了完整起见:

class Hotel {
    City city
    Boolean deleted = false
    Integer stars
}

class City {
    String name
}

集成测试(使用build-test-data plugin

import grails.test.*

class HotelTests extends GrailsUnitTestCase {

    void testCriteria() {
        City city1 = City.build(name:'one')
        assertNotNull(city1)
        City city2 = City.build(name:'two')
        assertNotNull(city1)

        Hotel fiveStarHotel= Hotel.build(city:city1, deleted:false, stars:5)
        assertNotNull(fiveStarHotel)

        Hotel hotelInCity2 = Hotel.build(city:city2, deleted:false, stars:5)
        assertNotNull(hotelInCity2)

        Hotel deletedHotel = Hotel.build(city:city1, deleted:true, stars:5)
        assertNotNull(deletedHotel)

        Hotel threeStarHotel = Hotel.build(city:city1, deleted:false, stars:3)
        assertNotNull(threeStarHotel)

        Hotel oneStarHotel = Hotel.build(city:city1, deleted:false, stars:1)
        assertNotNull(oneStarHotel)

        def c = Hotel.createCriteria()

        def hotelCount = c.get {
            projections {
                count('id')
            }
            gt("stars", 2)          
            eq("city", city1)           
            eq("deleted", false)

        }
        assertEquals(2, hotelCount) //should only find the 5 and 3 star hotel

        def c2 = Hotel.createCriteria()
        hotelCount = c2.count {
            gt("stars", 2)          
            eq("city", city1)           
            eq("deleted", false)

        }
        assertEquals(2, hotelCount) //should only find the 5 and 3 star hotel
    }
}

答案 1 :(得分:23)

域对象上有动态计数器和查找程序:

Hotel.countByCity(city)

user guide中的更多详细信息,当然