0

我是 Java 中 Spring 框架的新手。还有一些问题......
我的应用程序使用spring、spring sequrity、hibernate 框架。
我正在写一些简单的应用程序。
我有一个页面,可以通过休眠将用户添加到数据库。
用户等级:

@Entity
@Table(name="users")
public class User 
{
    @Id
    @GeneratedValue
    private Integer id;

    private String login;

    private String password;

    @OneToOne(cascade=CascadeType.ALL)
    @JoinTable(name="user_roles",
        joinColumns = {@JoinColumn(name="user_id", referencedColumnName="id")},
        inverseJoinColumns = {@JoinColumn(name="role_id", referencedColumnName="id")}
    )
    private Role role;


    private Integer moneyCount;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public Integer getMoneyCount() {
        return moneyCount;
    }

    public void setMoneyCount(Integer moneyCount) {
        this.moneyCount = moneyCount;
    }
}

角色类:

@Entity
@Table(name="roles")
public class Role {

 @Id
 @GeneratedValue
 private Integer id;

 private String role;

 @OneToMany(cascade=CascadeType.ALL)
 @JoinTable(name="user_roles", 
    joinColumns = {@JoinColumn(name="role_id", referencedColumnName="id")},
    inverseJoinColumns = {@JoinColumn(name="user_id", referencedColumnName="id")}
)
private Set<User> userRoles;

@Override
public int hashCode() {
    return new Long(id).hashCode();
}

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (! (obj instanceof Role)) {
        return false;
    }
    return this.id == ((Role)obj).getId();
}


public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getRole() {
    return role;
}

public void setRole(String role) {
    this.role = role;
}

public Set<User> getUserRoles() {
    return userRoles;
}

public void setUserRoles(Set<User> userRoles) {
    this.userRoles = userRoles;
}
}

保存在控制器中:

@RequestMapping(value = "/users/add", method = RequestMethod.POST)
public String addUser(@ModelAttribute("user")User user) {
    user.setPassword(StatickHelpers.md5(user.getPassword()));
    user.setId(15); // if remove this line i'll recive user fild id null exception
    userDAO.addUser(user);
    return "redirect:/users/";
}

保存在 UserDAO

 @Transactional
public void addUser(User user)
{
    Session session = null;
    try {
        session = openSession();

        session.save(user);

    } catch (Exception e) {

    } finally {

    }
}

我在页面上有一个选择块,我可以在其中选择用户角色作为字符串。
所以当我保存用户时

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.validation.BindException: 

org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'user' on field 'role': rejected value [ROLE_EMPLOYER]; codes [typeMismatch.user.role,typeMismatch.role,typeMismatch.com.sprsec.model.Role,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.role,role]; arguments []; default message [role]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.sprsec.model.Role' for property 'role'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.sprsec.model.Role] for property 'role': no matching editors or conversion strategy found]
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)

因此,据我所知,我应该在配置中创建一个 bean,以帮助 Spring 框架映射从字符串到角色对象的转换?有人可以帮我解决这个错误吗?

4

1 回答 1

0

尝试在方法中添加BindingResult bindingResult之后。@ModelAttribute("user")User useraddUser

因此,您的 addUser 方法将如下所示:

public String addUser(@ModelAttribute("user")User user, BindingResult bindingResult) {
    user.setPassword(StatickHelpers.md5(user.getPassword()));
    user.setId(15); // if remove this line i'll recive user fild id null exception
    userDAO.addUser(user);
    return "redirect:/users/";
}
于 2013-07-20T16:54:07.897 回答