简体   繁体   English

GORM grails withCriteria查询

[英]GORM grails withCriteria query

I have 3 domain: 1) user 2) profile 3) Doctor 我有3个域:1)用户2)个人资料3)医生

1) doctor extends user 2) user hasOne profile 1)医生扩展用户2)用户具有一个配置文件

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

class Profile {
    String fullName
    String address
    String cellno
}

class Doctor {
    String workLocation
    String specialization
}

How do I write a GORM query to list all the doctor based on specialization and workLocation with their names profile.fullName ? 如何编写一个GORM查询以根据专业化和workLocation列出所有医生及其名字profile.fullName

Since Doctor extends User , I'm assuming the Doctor domain class looks like this: 由于Doctor扩展了User ,因此我假设Doctor域类如下所示:

class Doctor extends User {
    String workLocation
    String specialization
}

You can get a list of Doctor instances with a GORM query like this: 您可以使用GORM查询获取Doctor实例的列表,如下所示:

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

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

The query above uses the eq() method to specify the WHERE criteria. 上面的查询使用eq()方法指定WHERE条件。 If you want the full names rather that Doctor instances you'll need a projection: 如果要使用全名而不是Doctor实例,则需要一个投影:

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

    projections {
        profile {
            property 'fullName'
        }
    }
}

The projection is essentially the SELECT part of the query. 投影实质上是查询的SELECT部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM