Grails:对关系中的集合进行分页和排序
我想从关系中的集合进行分页和排序
例如使用以下模型:
class User {
String userName, password
static hasMany = [roles: UserRole, preferences: Preference]
}
class UserRole {
String name, description
static hasMany = [actions: Action]
}
我想恢复特定用户的所有角色。我已经加载了用户,所以正常的方法是使用
user.roles
但我想按 UserRole
属性对它们进行排序,并且我想动态地对它们进行分页
,我知道如果我想获取所有 <我可以使用 code>UserRole 进行排序和分页:
UserRole.list([sort: 'name', order: 'asc',max: 5,offset:0])
但我只想为与我的用户关联的角色执行此操作。我试图使用标准,但我认为我遗漏了一些东西。
我也看了一下这里: http://grails.1312388.n4.nabble.com /A-Relationship-Paging-Pattern-td1326643.html
但是我必须添加关系回到 UserRole
所以我会:
static hasMany = [users : UserRole]
我该怎么做?最好的方法是什么?
如果您需要更多信息,请告诉我,如果我不够清楚,请告诉我
谢谢和问候
I'd like to do paging and sorting from a collection in a relationship
For example with the following model:
class User {
String userName, password
static hasMany = [roles: UserRole, preferences: Preference]
}
class UserRole {
String name, description
static hasMany = [actions: Action]
}
I'd like to recover all the roles for a specific user. I already have the user loaded so the normal way to do it would be using
user.roles
But I want to sort them by UserRole
properties and I want to paginate them dynamically
I know that if I want to get all the UserRole
s sorted and paginated I can use:
UserRole.list([sort: 'name', order: 'asc',max: 5,offset:0])
But I want to do it just for the roles that are associated to my user. I was trying to use criteria, but I think I'm missing something.
I also had a look here:
http://grails.1312388.n4.nabble.com/A-Relationship-Paging-Pattern-td1326643.html
But then I would have to add the relation back into UserRole
so I would have:
static hasMany = [users : UserRole]
How can I do this? what would be the best way?
Please, let me know if you need more information and sorry if I wasn't clear enough
Thanks and regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法对“普通”关系进行分页。
您可以使用映射 DSL 更改子对象的显示顺序:
要简化手工制作的分页关系,您可以使用命名查询:
或者您可以实现瞬态
User
的属性,该属性将为User 返回一个
的Criteria
角色
(可以分页)。You cannot paginate an "ordinary" relationship.
You can change the order child objects appear in using mapping DSL:
To simplify a hand-crafted paginated relationship, you can use a named query:
Or you can implement a transient
User
's property that will return aCriteria
forUser
'sRole
s (which can be paginated).Grails 分页与 hasmany 关系双向属性终于我说到了重点,我发现它工作了Huuuh。
这些是域类
现在这是我的控制器逻辑
和 gsp 代码
Grails Pagination with hasmany relation Bidirectional property finally i come to the point were i found it working Huuuh.
These are the Domain classes
Now This is my controller Logic
And the gsp code