grails grom用多对多的映射创建标准 [英] grails grom create criteria with many-to-many mapping
本文介绍了grails grom用多对多的映射创建标准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
class Book实现Serializable {
字符串bookName
时间戳createdDateTime
Blob文件
static belongsTo = [用户]
static hasMany = [user:用户]
}
我可以使用 addToUser()
方法。
但是我在创建条件时被卡在用户过滤器中。
def query = Book.createCriteria();
def results = query.list(){
eq(user,userObject)//不工作,因为用户字段是用户列表。
order(createdDateTime,desc)
}
请帮忙我使用正确的过滤方式。
解决方案
您需要首先以多对多关系连接用户表。条件应该如下所示:
Book.withCriteria {
user {
eq(id, userObject.id)
}
order(createdDateTime,desc)
}
I have two domain classes: User and Book.
class Book implements Serializable{
String bookName
Timestamp createdDateTime
Blob file
static belongsTo = [User]
static hasMany = [user :User]
}
I am able to add user in book using addToUser()
method.
But I am stuck in create criteria while applying filter in user.
def query = Book.createCriteria();
def results = query.list () {
eq("user",userObject) // not working since user field is a list of users.
order("createdDateTime", "desc")
}
Please help me with the correct way of filtering.
解决方案
You need to join the user table first in a many-to-many relation. The criteria should look like:
Book.withCriteria {
user {
eq("id", userObject.id)
}
order("createdDateTime", "desc")
}
这篇关于grails grom用多对多的映射创建标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文