1

使用分页的能力是目标,但是我不确定如何使用它。这种关系是单向的 hasMany(见下文)。目前它在没有分页的情况下可以正常工作(请参阅屏幕截图),但是要使用分页,我必须更改控制器以使用 createCriteria。这就是我遇到困难的地方,因为这种关系很奇怪。

领域

Class Tag {

      String Tag
      User user

      static hasMany = [events: Event]
}

控制器

def searchTags() {
    println("TAG CONTROLLER  -  Params sent on click of tag are:  "+params.selected)
    def results = Tag.findByTagAndUser(params.selected, lookupPerson())

    //resultTotal is for show for the time being 
    [query: params.selected, results: results, resultTotal: 0]
}

普惠制

<g:each in="${results.events}" status="i" var="t">
   .... ommited as unnessary
</g:each>

//Not working yet due to confusing createCriteria issue
<div class="pagination">
    <g:paginate total="${resultTotal}" />
</div>

我可以做一些指导,这是我到目前为止的想法,但我有点困惑:

    def tagCriteria = Tag.createCriteria()
    def result = tagCriteria.list (max: 50, offset: 10) {
        eq("tag", params.selected)
        and {
            eq("user", lookupPerson())
        }
        //Not sure what to do at this point compared with the findByTagAndUser(params.selected, lookupPerson()).events  events being the hasMany relationship seen in domain
        events {

        }
    }
4

1 回答 1

0

试试这个。

def searchTags() {        
    def tagCriteria = Tag.createCriteria()
    def results = tagCriteria.list (max: 50, offset: 10) {

        eq("tag", params.selected)
        eq("user", lookupPerson())            

        projections {
            property("events")
        }
    }

    [query: params.selected, results: results, resultTotal: results.totalCount]
}

HQL 版本 - 问题是您必须执行两次相同的查询,但选择count(e)

Tag.executeQuery("""
Select e
from Tag t join t.events as e
where t.tag = :selected
and t.user = :user
""", [selected: params.selected, user: lookupPerson()], [max: 50, offset 10])
于 2013-03-03T13:29:59.070 回答