代码之家  ›  专栏  ›  技术社区  ›  arturh

在Grails中查询多对多关系时出错

  •  2
  • arturh  · 技术社区  · 16 年前
    class Address {
        static hasMany = [addressGroups: AddressGroup]
        ...
    }
    
    class AddressGroup {
        static belongsTo = Address
        static hasMany = [addresses: Address]
    
        String notation;
        ...
    }
    

    我的代码:

    def params = [max: 10]
    def addressGroup = AddressGroup.findByNotation("asdf")
    def addresses = Address.findAllByAddressGroups(addressGroup, params)
    

    没有为参数2指定值

    select ... from ADDRESSES this_ where this_.id=10 order by this_.id asc limit ** NOT SPECIFIED **
    

    ... 这是完全错误的。

    我可以使用addressGroup.Addresses获取地址(这很有效!),但我想在带有分页的表中显示它,所以我需要max/offset参数。

    2 回复  |  直到 16 年前
        1
  •  1
  •   Jean Barmash    16 年前

    退房 GORM Labs Plugin ,它引入了对GORM的一些扩展。“分页HasMany属性”部分向您展示了如何完成您所寻找的内容。

        2
  •  1
  •   arturh    16 年前

    我使用标准API结束:

    def c = Address.createCriteria()
    def results = c.list {
       if(params.addressGroup) {
          addressGroups {
             eq('id', params.addressGroup)
          }
       }
    
       maxResults(params.max)
       order(params.sort, params.order)    
    }
    
    推荐文章