2015-11-08 57 views
0

我有3个域名: 1)用户 2)曲线 3)医生GORM Grails的withCriteria查询

1)医生扩展用户 2)用户hasOne轮廓

class User { 
    String username 
    String password 
    static hasOne = [profile: Profile] 
} 

class Profile { 
    String fullName 
    String address 
    String cellno 
} 

class Doctor { 
    String workLocation 
    String specialization 
} 

我怎样写一个GORM查询列出所有医生的专业化和workLocation与他们的名字profile.fullName

回答

0

由于Doctor延伸User,我假设Doctor域类看起来是这样的:

class Doctor extends User { 
    String workLocation 
    String specialization 
} 

你可以得到Doctor实例列表与GORM查询是这样的:

def location = 'someWorkLocation' 
def spec = 'someSpecialization' 

def doctors = Doctor.withCriteria { 
    eq 'specialization', spec 
    eq 'workLocation', location 
}.list() 

上面的查询使用eq()方法来指定WHERE标准。如果你想的全名宁可Doctor情况下,你需要一个投影:

def names = doctors.withCriteria { 
    eq 'specialization', spec 
    eq 'workLocation', location 

    projections { 
     profile { 
      property 'fullName' 
     } 
    } 
} 

的投影基本上是查询的SELECT一部分。