spring hibernate - 错误添加数据@OneToMany @ManyToOne错误

时间:2013-11-11 15:21:12

标签: mysql spring hibernate jpa annotations

我有两个表: USERS ROLES_MAP ,以及它们之间的FK。向这些表添加数据时,用户表看起来不错,但表 roles_map 中的 username_u 缺失。有人能告诉我哪里出错了吗?

User.java

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "users")
public class User {
    @Id
    @Column(name = "USER_ID", unique = true, nullable = false)
    private int user_id;
    @Column(name = "NAME", nullable = false)
    private String name;
    @Column(name = "SURNAME", unique = true, nullable = false)
    private String surname;
    @Column(name = "USERNAME_U", unique = true, nullable = false)
    private String username_u; // zamiast username
    @Column(name = "PASSWORD", unique = true, nullable = false)
    private String password;
    @Column(name = "USER_DESCRIPTION", nullable = false)
    private String userDescription;
    @Column(name = "AUTHORITY", nullable = false)
    private String authority = "ROLE_USER";
    @Column(name = "ENABLED", nullable = false)
    private int enabled;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "rUser", cascade={CascadeType.ALL})
    private List<RolesMap> rolesMap;

    public List<RolesMap> getRolesMap() {
        return rolesMap;
    }

    public void setRolesMap(List<RolesMap> rolesMap) {
        this.rolesMap = rolesMap;
    }

    /**
     * @return the user_id
     */
    public int getUser_id() {
        return user_id;
    }

    /**
     * @param user_id
     *            the user_id to set
     */
    public void setUser_id(int user_id) {
        this.user_id = user_id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name
     *            the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the surname
     */
    public String getSurname() {
        return surname;
    }

    /**
     * @param surname
     *            the surname to set
     */
    public void setSurname(String surname) {
        this.surname = surname;
    }

    /**
     * @return the username_u
     */
    public String getUsername_u() {
        return username_u;
    }

    /**
     * @param username_u
     *            the username_u to set
     */
    public void setUsername_u(String username_u) {
        this.username_u = username_u;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password
     *            the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the userDescription
     */
    public String getUserDescription() {
        return userDescription;
    }

    /**
     * @param userDescription
     *            the userDescription to set
     */
    public void setUserDescription(String userDescription) {
        this.userDescription = userDescription;
    }

    /**
     * @return the authority
     */
    public String getAuthority() {
        return authority;
    }

    /**
     * @param authority
     *            the authority to set
     */
    public void setAuthority(String authority) {
        this.authority = authority;
    }

    /**
     * @return the enabled
     */
    public int getEnabled() {
        return enabled;
    }

    /**
     * @param enabled
     *            the enabled to set
     */
    public void setEnabled(int enabled) {
        this.enabled = enabled;
    }

    @Override
    public String toString() {
        StringBuffer strBuff = new StringBuffer();
        strBuff.append("id : ").append(getUser_id());
        strBuff.append(", name : ").append(getName());
        strBuff.append(", surname : ").append(getSurname());
        return strBuff.toString();
    }
}

RolesMap.java

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import com.patpuc.model.User;

@Entity
@Table(name = "roles_map")
public class RolesMap {
    private int rm_id;
    private String username_a;
    private String username_l;
    private String password;
    private int role_id;

    private User User;
    @ManyToOne
    @JoinColumn(name = "username_u", nullable = false)
    public User getrUser() {
        return User;
    }

    public void setrUser(User User) {
        this.User = User;
    }

    @Id
    @Column(name = "RM_ID", unique = true, nullable = false)
    public int getRmId() {
        return rm_id;
    }

    public void setRmId(int rm_id) {
        this.rm_id = rm_id;
    }

    @Column(name = "USERNAME_A", unique = true)
    public String getUsernameA() {
        return username_a;
    }

    public void setUsernameA(String username_a) {
        this.username_a = username_a;
    }

    @Column(name = "USERNAME_L", unique = true)
    public String getUsernameL() {
        return username_l;
    }

    public void setUsernameL(String username_l) {
        this.username_l = username_l;
    }

    @Column(name = "PASSWORD", unique = true, nullable = false)
    public String getPassword() {
        return password;
    }

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

    @Column(name = "ROLE_ID", unique = true, nullable = false)
    public int getRoleId() {
        return role_id;
    }

    public void setRoleId(int role_id) {
        this.role_id = role_id;
    }

}

添加方法

private int id;
    private String name;
    private String surname;
    private String password;
    private String userDescription;
    private String username_u;
    private String authority;
    private int enabled;
    private List<RolesMap> rolesMap;
    // Selected users that will be removed
    private User[] selectedUsers;

    public String addUser() {
        try {
            RolesMap roles = new RolesMap();
            User user = new User();

            roles.setPassword(getPassword());
            roles.setRoleId(2);

            user.setUser_id(getId());
            user.setName(getName());
            user.setSurname(getSurname());
            user.setUsername_u(getUsername_u());
            user.setPassword(getPassword());
            user.setUserDescription(getUserDescription());
            user.setAuthority(getAuthority());
            user.setEnabled(getEnabled());
            user.setRolesMap(new ArrayList<RolesMap>());
            user.getRolesMap().add(roles);

            getUserService().addUser(user);
            return SUCCESS;
        } catch (DataAccessException e) {
            e.printStackTrace();
        }

        return ERROR;
    }

有什么想法吗?

0 个答案:

没有答案