如何使用 Grails ORM 检索具有最大、偏移和排序参数的列表
How to retrieve list with max, offset and sort parameters with Grails ORM
我有一个带有 User
和 Group
域对象的 grails 应用程序。 User
有许多 Group
个对象,Group
对象包含许多 User
个对象:
class User implements Serializable {
static constraints = {
usergroups nullable: true
}
static mapping = {
usergroups cascade: 'all-delete-orphan'
}
static hasMany = [
usergroups: Group
]
static mappedBy = [
usergroups : "creator"
]
}
class Group {
static belongsTo = [
creator : User
]
static hasMany = [
members : User
]
static constraints = {
creator nullable: false
members nullable: true, maxSize: 100
}
}
给定一个 Group
对象,我可以使用 max
、offset
和 sortBy
参数检索成员吗?像...
def members = User.where {
/* how to specify only the users in 'group.members'? */
}.list(
max: max,
offset: offset,
sortBy : sortBy
);
编辑
为了尝试解决问题,我更改了 User
class 以包含 joinedgroups
字段...
class User implements Serializable {
static constraints = {
usergroups nullable: true
joinedgroups nullable: true
}
static mapping = {
usergroups cascade: 'all-delete-orphan'
}
static hasMany = [
usergroups: Group
joinedgroups: Group
]
static mappedBy = [
usergroups : "creator",
joinedgroups : "creator" // if I don't add this Grails complains there is no owner defined between domain classes User and Group.
]
}
但是现在,当我尝试在我的应用程序的另一部分检索所有用户的 usergroup
对象时,只返回一个用户组...
def groups = Group.where {
creator.id == user.id
}.list(max: max, offset: offset, sort: sortBy); // should return 3 groups but now only returns 1
此查询之前有效,所以可能在 User
中添加额外的 mappedby
条目导致了问题。 User
中的新 mappedby
字段是否不正确?
如果包含用户的所有组都保存在 usergroups
字段中,您可以使用:
def query = User.where {
usergroups { id == myGroup.id }
}
def users = query.list(max: 10, offset: 0, sort : "id")
我有一个带有 User
和 Group
域对象的 grails 应用程序。 User
有许多 Group
个对象,Group
对象包含许多 User
个对象:
class User implements Serializable {
static constraints = {
usergroups nullable: true
}
static mapping = {
usergroups cascade: 'all-delete-orphan'
}
static hasMany = [
usergroups: Group
]
static mappedBy = [
usergroups : "creator"
]
}
class Group {
static belongsTo = [
creator : User
]
static hasMany = [
members : User
]
static constraints = {
creator nullable: false
members nullable: true, maxSize: 100
}
}
给定一个 Group
对象,我可以使用 max
、offset
和 sortBy
参数检索成员吗?像...
def members = User.where {
/* how to specify only the users in 'group.members'? */
}.list(
max: max,
offset: offset,
sortBy : sortBy
);
编辑
为了尝试解决问题,我更改了 User
class 以包含 joinedgroups
字段...
class User implements Serializable {
static constraints = {
usergroups nullable: true
joinedgroups nullable: true
}
static mapping = {
usergroups cascade: 'all-delete-orphan'
}
static hasMany = [
usergroups: Group
joinedgroups: Group
]
static mappedBy = [
usergroups : "creator",
joinedgroups : "creator" // if I don't add this Grails complains there is no owner defined between domain classes User and Group.
]
}
但是现在,当我尝试在我的应用程序的另一部分检索所有用户的 usergroup
对象时,只返回一个用户组...
def groups = Group.where {
creator.id == user.id
}.list(max: max, offset: offset, sort: sortBy); // should return 3 groups but now only returns 1
此查询之前有效,所以可能在 User
中添加额外的 mappedby
条目导致了问题。 User
中的新 mappedby
字段是否不正确?
如果包含用户的所有组都保存在 usergroups
字段中,您可以使用:
def query = User.where {
usergroups { id == myGroup.id }
}
def users = query.list(max: 10, offset: 0, sort : "id")