Java,Spring将一个值从一个类映射到另一个类的值

时间:2013-07-20 13:15:43

标签: java spring spring-mvc

我是Java的Spring framwork的新手。并有一些问题...
我的应用程序使用spring,spring sequrity,hibernate框架 我正在写一些简单的应用程序 我有一个页面,用户可以通过hibernate添加到db 用户类:

@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框架将convertion从string转换为Role对象? 有人可以帮我解决这个错误吗?

1 个答案:

答案 0 :(得分:0)

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

所以,你的addUser方法将是:

addUser