Grails - findAll加入两个表

时间:2015-02-10 16:03:08

标签: grails gorm findall

我是Grails的新手。我在从DB检索数据时遇到问题。我有类的域名,比如......

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

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

class UserAccount {
   String username
   ...
}

我想列出所有" Lightboxes"由ID = 4的用户拥有。我在尝试

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

def my_lb = Lightbox.findAllByUsers(4)

这些都不适合我。我究竟做错了什么? THX

2 个答案:

答案 0 :(得分:0)

试试这个:

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

答案 1 :(得分:0)

所以我做的略有不同,使用标准代替。看看是否感兴趣

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

似乎工作正常。感谢您的回复