Search code examples
grailsgrails-ormfindall

Grails - findAll joining two tables


I'm new to Grails. I'm having problem with retrieving data from DB. I have domains with classes, something like...

class Lightbox {    
   String name = ''
   String link  = ''
   static hasMany = [users: LightboxUserAccount]
}

class LightboxUserAccount {
   UserAccount userAccount
   static belongsTo = [lightbox: Lightbox]  
}

class UserAccount {
   String username
   ...
}

I want to list all "Lightboxes" owned by user with ID=4. I was trying

def my_lb = Lightbox.findAll("from Lightbox as lb where lb.users=:userAccount", [userAccount: springSecurityService.getCurrentUser()])

or

def my_lb = Lightbox.findAllByUsers(4)

None of those work for me. What am I doing wrong? Thx


Solution

  • So I did it slightly different, using criteria instead. Take a look if interested

    static getAllByUserAccount(UserAccount userAccount) {
        def criteria = Lightbox.createCriteria()
        def my_lb = criteria.list {
            users {
                eq('userAccount', userAccount)
            }
        }
        return my_lb
    } 
    

    It seems to be working. Thanks for responses though