1

I have one many-to-many relationship in my app which looks like this: class

App {
    String type
    String name

    static hasMany = [users: User]
}

class User {
    String email
    String regid

    static belongsTo = App
    static hasMany = [apps:App]

}

Now i want to access all Users of an app, by having its id. I prefer using plain SQL as It is working way faster and I database will have ~one million records. So i need a way to access all users of an app where app id in (array of given apps).

What I am trying is doing something like this:

def result = sql.eachRow('SELECT App_Users from App where id in(?)' [id:params.app_checkbox])

But this throws me exception as column App_users is not available.

I am trying to do this for 4-5 hours and bearing in mind I am not a web developer, it is already driving me crazy, so could you guys give me advice, how to access members of many-to-many relationship with sql?

user3482211
  • 446
  • 4
  • 17
  • Possible duplicated: http://stackoverflow.com/questions/2238807/many-to-many-link-tables-in-grails-gorm-hibernate and http://stackoverflow.com/questions/2477903/access-relationship-table-in-grails – Carvo Jun 18 '15 at 15:38

1 Answers1

1

At last i managed to achieve it with simple criteria:

 def results = App.createCriteria().list() {
                createAlias('users', 'user')
                'in'("id", goodIds)
                projections {
                    count('users')
                }
            }
user3482211
  • 446
  • 4
  • 17