【问题标题】:Can we give multiple things in the findAll我们可以在 findAll 中给出多个东西吗
【发布时间】:2015-06-06 11:40:55
【问题描述】:

我有一个 Employee 域,其中包含 Professional_id。专业领域中有一个称为级别的字段。现在我想提取所有处于同一级别的员工。如何编写查询?

我想要类似的东西

def emp = Employee.findAllByProfessional_id(Professional.findAllByLevel(5))

员工域-:

class Employee {
    String username
    String password
    String reg_no
    String designation
    String personal_id
    String professional_id
    static constraints = {
        username(unique:true)
        password(password:true)
    }
}

专业领域-:

class Professional {
    int level
    String bank_account_no
    String qualification
    String salary
    String department
    String performance
    String basic_pay
    String fax_no
    String employee_awards
    String job_duration
    String work_shift

    static constraints = {
        bank_account_no(unique:true)
    }
}

【问题讨论】:

    标签: mongodb grails


    【解决方案1】:

    Professional.findAllByLevel(5) 将返回 Professional 对象列表,但 Employee.findAllByProfessional_id() 正在等待字符串列表,因为属性 professional_id 是Employee 类中的字符串。因此,您尝试执行的操作将行不通。

    尝试这样做:

    def emp = Employee.findAllByProfessional_id(Professional.findAllByLevel(5).id.toString())
    

    无论如何,您的域似乎没有正确定义。您不需要像在 Employee 类中那样指定一个 professional_id 字段。您应该使用 belongsTo、hasMany、hasOne 来指定关联。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多