Grails undirectional hasMany createCriteria

时间:2013-03-03 00:29:41

标签: grails gorm grails-2.0

使用分页的能力是目标,但我不知道如何使用它。关系是单向的hasMany(见下文)。目前它的工作正常没有分页(参见Screenshot),但要使用分页,我必须更改控制器以使用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]
}

GSP

<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 {

        }
    }

1 个答案:

答案 0 :(得分: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])