【发布时间】:2012-12-08 12:19:13
【问题描述】:
我有 5 张桌子:
User (Mark)
Enterprise (Google)
EnterpriseUser (Mark, Google)
EnterpriseRight (LOGIN)
EnterpriseUserRight (Mark-Google, LOGIN)
我想使用 GORM 查询 Mark (Current User) 拥有 LOGIN 权限的所有企业的数据库。然后我希望能够对结果进行排序和分页。
我之前是通过构建一个 List 来实现的,但我指的是返回一个 ResultSet(仍然可以排序/分页/查询)
我的 User.class(域对象)尝试(生成 List
public List<Enterprise>getEnterprises() {
def list = new ArrayList<Enterprise>()
for (EnterpriseUser eu: this.enterpriseUsers) {
if (this.hasEnterpriseRights(eu.enterprise, [EnterpriseRight.LOGIN])) {
list.add(eu.enterprise)
}
}
return list
}
(但我无法对这些结果进行排序和分页,因为它们现在是一个列表。如果这个查询可以返回一个结果集就好了)
英文:
查找与此User 关联的所有Enterprise,其中此用户的EnterpriseRight 具有字符串名称“LOGIN”。按字母顺序列出,从偏移量 10 给我 10 个结果。
我会在这里发布我的代码,但我不知道从哪里开始。
域对象是:
class User {
static hasMany = [userSystemRights: UserSystemRight, enterpriseUsers: EnterpriseUser]
String email;
String passwordHash;
}
class Enterprise {
static hasMany = [enterpriseUsers: EnterpriseUser, measurements: Measurement]
String name = ""
String tradingName = ""
}
class EnterpriseUser {
static belongsTo = [enterprise: Enterprise, user: User]
static hasMany = [enterpriseUserRights: EnterpriseUserRight]
Enterprise enterprise
User user
String interest
}
class EnterpriseUserRight {
static belongsTo = [enterpriseUser: EnterpriseUser, enterpriseRight: EnterpriseRight]
EnterpriseUser enterpriseUser
EnterpriseRight enterpriseRight
}
class EnterpriseRight {
public static final String LOGIN = "LOGIN"
public static final String MANAGE_USERS = "MANAGE_USERS"
public static final String MANAGE_SETTINGS = "MANAGE_SETTINGS"
static hasMany = [enterpriseUserRights: EnterpriseUserRight]
String name
}
【问题讨论】:
-
Hibernate 查询实体和关联,而不是表。向我们展示您如何将这些表映射到实体和关联。
-
嘿@JBNizet,感谢您的提问!我已经更新了问题。
标签: hibernate grails hql grails-orm