具有嵌套属性提交的模型满足验证错误

时间:2015-01-12 07:17:12

标签: spring spring-mvc

我有两个模型EmployeeDepartment,它们之间存在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

发生了什么?

0 个答案:

没有答案