WebDataBinder和Set.Class的问题

时间:2011-04-20 02:47:29

标签: spring spring-mvc

我在从表单绑定数据时遇到问题: 我有两个班级

@Entity
@Table(name = "ROLES")
public class Role implements GenericDomain {

private Long    id;
private String  code;
private String  name;

private Set<Privilege> privileges = new HashSet<Privilege>(0);

public Role() {}

/* getter and setter*/ 

@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name = "ROLES_PRIVILEGES"
    , joinColumns = { @JoinColumn(name = "ROLE_ID") }
    , inverseJoinColumns = { @JoinColumn(name = "PRIVILEGE_ID") }
)

public Set<Privilege> getPrivileges() {
    return this.privileges;
}
public void setPrivileges(Set<Privilege> privileges) {
    this.privileges = privileges;
}
    /*  overide of hascode, equals*/ 
}

并且

@Entity
@Table(name = "PRIVILEGES")
public class Privilege implements GenericDomain {

private Long    id;
private String  code;

private Set<Role> roles = new HashSet<Role>(0);

public Privilege() {}

 /* getter and setter*/

 @ManyToMany(cascade=CascadeType.REFRESH, mappedBy="privileges")
public Set<Role> getRoles() {
    return this.roles;
}
public void setRoles(Set<Role> roles) {
    this.roles = roles;
}
@Override
public String toString(){
    return this.getCode() + this.getComment();
    } 

/*overide equals and hascode*/

在我的控制器中我有:

    @InitBinder
public void initBinder(WebDataBinder binder) {

   binder.registerCustomEditor(Set.class, "privileges", new CustomCollectionEditor(Set.class) {
       @Override 
       protected Object convertElement(Object element) {
            return (element == null)?null:privilegeService.getOne(Integer.parseInt((String)element));
        }
    });
}
    @RequestMapping(value = "edit", method = RequestMethod.POST)  
public String saveOldRole(  @ModelAttribute("role") Role role
                            , BindingResult result
                            , ModelMap model
                        ) {

    validator.validate(role, result);
    if (result.hasErrors()){
        logger.error(result.getAllErrors());
        model.addAllAttributes(result.getModel());
        return "/admin/role/edit";
    }
    logger.info(role.getPrivileges());
    Iterator p = role.getPrivileges().iterator();
    while(p.hasNext()){
        logger.info(p.next().getClass());
    }
    roleService.saveOrUpdate(role);
    model.addAttribute("roles", roleService.getAll());
    sessionStatus.setComplete();
    return "redirect:/admin/role/list.do";
}

我的调试是

role.RoleController:93 - [[MANAGE_USERS], [MANAGE_ROLES]]
role.RoleController:96 - class java.util.LinkedHashSet
role.RoleController:96 - class java.util.LinkedHashSet
22:29:44,915 ERROR tomcat-http--7 property.BasicPropertyAccessor:194 - IllegalArgumentException in class: com.stunaz.domain.Privilege, getter method of property: id

我不明白为什么在96,类类型是java.util.LinkedHashSet,我认为它应该是Privileges。 我不明白为什么我的role.getPrivileges()是一套Set,它应该是一组特权。

当然在saveOrUpdate收到错误。

1 个答案:

答案 0 :(得分:0)

finaly !!! 根本没有错误! 我将我的spring jar从3.0.5.RELEASE更新为3.1.0.M1,并且瞧:somthing停止使用webdatabinder和CustomCollectionEditor。

我只是回滚到3.0.5.RELEASE,一切都很好。