2013-11-21 40 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;