2013-11-21 42 views
1

我想更新一个实体,该实体具有其他实体的一对多List集合。处理程序方法被调用时,验证似乎不在集合上运行。我已阅读文档,并搜索了stackoverflow,但没有发现任何有用的东西。Spring 3 mvc @Valid注解不适用于列表<Entity>属性

型号:

@Entity 
public class Employee { 
    @Id 
    @GeneratedValue 
    private int employeeId; 
    @NotEmpty 
    private String name;   
    @Min(value=18) 
    private int age;   
    @OneToMany(mappedBy="parent",cascade=CascadeType.ALL) 
    private List<Child> children; 
//getters,setters 
} 
@Entity 
public class Child { 
    @Id 
    @GeneratedValue 
    private int childId; 
    @Column(nullable=false) 
    @NotNull 
    @Size(min=1,message="Child's name must not be empty") 
    private String childName; 
    @Max(value=18) 
    private Integer age; 
    @ManyToOne 
    @JoinColumn(name="employeeId") 
    private Employee parent; 
//getters,setters 
} 

在控制器:

@RequestMapping(value = { "/edit/{id}" }, method = RequestMethod.POST) 
    private String update(@PathVariable int id, ModelMap model, @Valid Employee employee, BindingResult result) { 
     if (result.hasErrors()) { 
      return "employee/edit"; 
     } 
     employeeDao.merge(employee); 
     return "redirect:../list"; 
    } 

验证工作的员工bean的简单性,而不是在孩子列表中的元素。

这怎么解决?

回答

6

看起来像你应该装饰children列表与@Valid注释,如描述here

它应该是这个样子:

@OneToMany(mappedBy="parent",cascade=CascadeType.ALL) 
@Valid 
private List<Child> children;