【问题标题】:GORM grails withCriteria query带有条件查询的 GORM grails
【发布时间】:2015-11-08 15:43:33
【问题描述】:

我有 3 个域: 1) 用户 2) 简介 3) 医生

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
}

如何编写一个 GORM 查询以列出所有基于专业和workLocation 的医生及其姓名profile.fullName

【问题讨论】:

    标签: grails grails-orm


    【解决方案1】:

    由于Doctor 扩展User,我假设Doctor 域类如下所示:

    class Doctor extends User {
        String workLocation
        String specialization
    }
    

    您可以使用这样的 GORM 查询获得Doctor 实例的列表:

    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 部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-03
      相关资源
      最近更新 更多