Can you combine controller list params with Hibernate criteria?

。_饼干妹妹 提交于 2019-12-08 02:27:29

问题


I have noticed that you can pass "params" straight in to the boilerplate code below:

[fooInstanceList: Foo.list(params), fooInstanceTotal: Foo.count()]

Is it possible to pass "params" in as part of a Hibernate criteria for example the one below?

def c = Foo.createCriteria()
    def results = c {
        not { eq("bar","test") }
    }

    [fooInstanceList: results, fooInstanceTotal: results.size()]

I am looking to use the "max" and "offset" params so I can use it for paging for example. I would also like to use the equivalent of count that counts all non-paged results. I think results.size() would only give me paged results, instead of the desired non-paged results. How would I go about this?


回答1:


You can use params while using the criteria. I suppose you have a typo of not using c.list

def c = Foo.createCriteria()
def results = c.list(params) {
    not { eq("bar","test") }
}

Assuming params has max and offset.

Criteria returns a PagedResultList where you can get the totalCount from it. So

results.totalCount //results.getTotalCount()

should give you the total count, although there is always a second query fired to get the total count. In this case Hibernate does that for you instead of you doing it explicitly.



来源:https://stackoverflow.com/questions/19186515/can-you-combine-controller-list-params-with-hibernate-criteria

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!