我有两个模型Employee
和Department
,它们之间存在many-one
关系。
@Entity
public class Employee {
@NotEmpty
@ManyToOne
@JoinColumn(name = "department_id")
private Department department;
}
@Entity
public class Department {
@OneToMany(cascade = CascadeType.ALL, mappedBy = "department")
private Set<Employee> employees;
}
Employee
的控制器:
@Controller
@RequestMapping("/employees")
public class EmployeeController {
@RequestMapping(value = "/", method = RequestMethod.POST)
public String addSubmit(@Valid Employee employee, BindingResult result) {
if (result.hasErrors()) {
return "employee/add";
} else {
employeeService.save(employee);
return "redirect:/employees/" + employee.getId();
}
}
}
添加员工表格:
<div class="control-group ${status.error ? 'error' : ''}">
<label class="control-label">Department</label>
<div class="controls">
<form:select path="department.id" items="${departments}" itemValue="id" itemLabel="name"/>
<span class="help-inline">${status.errorMessage}</span>
</div>
</div>
提交表单后,我收到错误:HV000030: No validator could be found for type: test.simple.model.Department.
似乎spring尝试自动创建employee
对象哪个容器为department
属性,并且无法验证department
。
但是我的Department
对于属性有NotNull
NotEmpty
这样的有效注释。
我可以通过表单添加Department
。
发生了什么?