我的员工域有一个关联的部门,部门和职位,这些都是域本身。这种关系编码为
class Employee
{
...
String firstName
String lastName
Position position
Division division
Department department
...
}
(请注意,没有belongsTo
或hasOne
关系)我使用
<g:select name="department" from="${Department.list()}" optionKey="id" optionValue="${{it.name}}" />
(类似于分区和位置),但是我在查询数据库的代码时遇到错误
def employeeList = Employee.createCriteria().list(sort: params.sort, order: params.order)
{
and
{
ilike "firstName", "%${params.firstName}%"
ilike "lastName", "%${params.lastName}%"
}
position
{
eq "position", ${params.position}
}
department
{
eq "department", ${params.department}
}
division
{
eq "division", ${params.division}
}
}
我得到的错误是
No signature of method: EmployeeController.department() is applicable for argument types: (EmployeeController$_results_closure1_closure4) values: [EmployeeController$_results_closure1_closure4@f559db0]
我已经尝试在and和block中放入部门/部门/位置eq
代码段,但是因错误而失败
No signature of method: EmployeeController.and() is applicable for argument types: (EmployeeController$_results_closure1_closure4) values: [EmployeeController$_results_closure1_closure4@2f3302f3]
任何想法我做错了什么?
答案 0 :(得分:1)
您需要在与.list()
调用相同的行上启动条件闭包。闭包应该是list()
方法的参数,但是当它在下一行开始时,Groovy正在执行list()
,然后尝试创建一个闭包。
def employeeList = Employee.createCriteria().list(sort: params.sort, order: params.order) {
and {
ilike "firstName", "%${params.firstName}%"
ilike "lastName", "%${params.lastName}%"
}
position {
eq "position", params.position
}
department {
eq "department", params.department
}
division {
eq "division", params.division
}
}
这相当于下面(注意括号)
def employeeList = Employee.createCriteria().list([sort: params.sort, order: params.order], {
and {
ilike "firstName", "%${params.firstName}%"
ilike "lastName", "%${params.lastName}%"
}
position {
eq "position", params.position
}
department {
eq "department", params.department
}
division {
eq "division", params.division
}
})
另请注意,您使用的是GString语法,而"
条件中没有必要的eq
。