Grails GORM:通过关联查找所有内容
Grails GORM : Find All with Associations
我正在尝试获取特定年龄范围内的所有用户。
我有一个 User
table 和一个 Profile
table 每个 User
.
最小 User
域 Class:
class User {
static hasOne = [profile: Profile]
}
最小 Profile
域 Class:
class Profile {
static belongsTo = [user: User]
Integer age
static constraints = {
age(range: 18 .. 150)
}
}
最小 UserService
域 Class:
class UserService {
def list() {
def users = User.findAll{
gender in whichGenderList
profile.age in 18 .. 150 /* Problem */
}
}
在 Problem
线上,我在浏览器中遇到以下异常:
URI /foo/browse
Class org.hibernate.QueryException
Message could not resolve property: profile_alias0 of: foo.Profile
问题:
在 UserService
我如何 select 从每个 User
的 Profile
table 它的年龄?
我正在使用:
- Grails 2.4.4
谢谢!
如有疑问,请使用 HQL:
User.findAll(
"from User u where u.gender in :genders and u.profile.age between :minAge and :maxAge",
[genders: [], minAge: 18, maxAge: 150]
)
像这样使用条件查询:
def users = User.createCriteria().list(){
inList('gender', whichGenderList)
profile{
inList('age', 18 .. 150)
}
}
我正在尝试获取特定年龄范围内的所有用户。
我有一个 User
table 和一个 Profile
table 每个 User
.
最小 User
域 Class:
class User {
static hasOne = [profile: Profile]
}
最小 Profile
域 Class:
class Profile {
static belongsTo = [user: User]
Integer age
static constraints = {
age(range: 18 .. 150)
}
}
最小 UserService
域 Class:
class UserService {
def list() {
def users = User.findAll{
gender in whichGenderList
profile.age in 18 .. 150 /* Problem */
}
}
在 Problem
线上,我在浏览器中遇到以下异常:
URI /foo/browse
Class org.hibernate.QueryException
Message could not resolve property: profile_alias0 of: foo.Profile
问题:
在 UserService
我如何 select 从每个 User
的 Profile
table 它的年龄?
我正在使用:
- Grails 2.4.4
谢谢!
如有疑问,请使用 HQL:
User.findAll(
"from User u where u.gender in :genders and u.profile.age between :minAge and :maxAge",
[genders: [], minAge: 18, maxAge: 150]
)
像这样使用条件查询:
def users = User.createCriteria().list(){
inList('gender', whichGenderList)
profile{
inList('age', 18 .. 150)
}
}