我的应用程序中有一个多对多关系,如下所示:class
App {
String type
String name
static hasMany = [users: User]
}
class User {
String email
String regid
static belongsTo = App
static hasMany = [apps:App]
}
现在,我想通过拥有其ID来访问应用程序的所有用户。我更喜欢使用普通的SQL,因为它的运行速度更快,并且我的数据库将有大约一百万条记录。所以我需要一种方法来访问应用程序的所有用户,其中应用程序ID位于(给定应用程序的数组)中。
我正在尝试做这样的事情:
def result = sql.eachRow('SELECT App_Users from App where id in(?)' [id:params.app_checkbox])
但这引发了异常,因为App_users列不可用。
我正在尝试进行4-5个小时的操作,并牢记我不是Web开发人员,这已经使我发疯,所以你们能给我建议,如何使用sql访问多对多关系成员?
最佳答案
最后,我设法用简单的标准实现了它:
def results = App.createCriteria().list() {
createAlias('users', 'user')
'in'("id", goodIds)
projections {
count('users')
}
}
关于grails - Grails访问关系表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29985768/