Search code examples
grailsgrails-orm

Retrieve all Products in a set of Categories - uni-directional and use pagination?


in our application we have a uni-directional relation like Category / Product

A Category has many Products, but a product does not know in what categories it is placed. Now i would like to retrieve all products for a set of categories and use the params for pagination I seem to hit a wall and have no clue on how to achieve this.

example:

class Category {
    static hasMany = [products: Product]
}

class Product {
}

Any hints on how i could achieve this?


Solution

  • It can be done using HQL like:

    def query = "select product from Category category join category.products product where     category.name in :categories"
    def books = Category.executeQuery(query, [categories:['Fantasy']])
    
    println books