arturh
arturh

Reputation: 6106

Error while Querying a many-to-many relationship in Grails

class Address {
    static hasMany = [addressGroups: AddressGroup]
    ...
}

class AddressGroup {
    static belongsTo = Address
    static hasMany = [addresses: Address]

    String notation;
    ...
}

I try to fetch all Addresses for a given AddressGroup.

My code:

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

The error message: No value specified for parameter 2

The logged SQL statement:

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

... which is completly wrong.

Any ideas?

I could fetch the Addresses by using addressGroup.addresses (and that works!), but I want to display it in a table with pagination so i need to max / offset params.

Upvotes: 2

Views: 678

Answers (2)

arturh
arturh

Reputation: 6106

Well ... I tried using the GORM extension, but after looking at it a bit closer I saw it has not what i need.

I endet up using the Criteria 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)    
}

Upvotes: 1

Jean Barmash
Jean Barmash

Reputation: 4788

I don't think the dynamic finders take the pagination parameters you need.

Check out GORM Labs Plugin, which introduces some extensions to GORM. Section "Paginated HasMany Properties" shows you how to do what you are looking for.

Upvotes: 1

Related Questions