开发者

Hibernate Mappings for struts2

I am trying to build a login page using hibernate and struts2. My design is as follows. Each login user has a role. Many users can have the same role.

So my class are:

User.java

@Entity
@Table(name = "user", catalog = "ciner")
public class User implements java.io.Serializable {

    private Integer userId;
    private Role role;
    private String loginId;
    private String password;
    private String firstName;
    private String lastName;

    public 开发者_如何学运维User() {
    }

    public User(Role role, String loginId, String password, String firstName, String lastName) {
        this.role = role;
        this.loginId = loginId;
        this.password = password;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "user_id")
    public Integer getUserId() {
        return this.userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

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

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "role_id")
    public Role getRole() {
        return this.role;
    }

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

    @Column(name = "login_id", nullable = false, length = 7)
    public String getLoginId() {
        return this.loginId;
    }

    public void setLoginId(String loginId) {
        this.loginId = loginId;
    }

    @Column(name = "first_name", nullable = false, length = 50)
    public String getFirstName() {
        return this.firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @Column(name = "last_name", nullable = false, length = 50)
    public String getLastName() {
        return this.lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return "User{" + "userId=" + userId + "role=" + role + "loginId="
                + loginId + "firstName=" + firstName + "lastName=" + lastName
                + '}';
    }

}

Role.java

@Entity
@Table(name = "role", catalog = "ciner")
public class Role implements java.io.Serializable {

    private Integer roleId;
    private String roleDescription;
    private List users;

    public Role() {
    }

    public Role(String roleDescription) {
        this.roleDescription = roleDescription;
    }

    public Role(String roleDescription, List users) {
        this.roleDescription = roleDescription;
        this.users = users;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "role_id", unique = true, nullable = false)
    public Integer getRoleId() {
        return this.roleId;
    }

    public void setRoleId(Integer roleId) {
        this.roleId = roleId;
    }

    @Column(name = "role_description", nullable = false, length = 100)
    public String getRoleDescription() {
        return this.roleDescription;
    }

    public void setRoleDescription(String roleDescription) {
        this.roleDescription = roleDescription;
    }

    // @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy =
    // "role")
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "role", targetEntity = User.class)
    public List getUsers() {
        return this.users;
    }

    public void setUsers(List users) {
        this.users = users;
    }

    @Override
    public String toString() {
        return "Role{" + "roleId=" + roleId + "roleDescription="
                + roleDescription + "users=" + users + '}';
    }
}

It returns 1 user just fine if it is valid. But since the method return that user back, i use return users.get(0);

Stack

java.lang.StackOverflowError
    java.lang.Integer.toString(Unknown Source)
    java.lang.Integer.toString(Unknown Source)
    java.lang.String.valueOf(Unknown Source)
    java.lang.Integer.toString(Unknown Source)
    java.lang.String.valueOf(Unknown Source)
    java.lang.StringBuilder.append(Unknown Source)
    com.inhis.model.Role.toString(Role.java:70)
    sun.reflect.GeneratedMethodAccessor293.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:197)
    com.inhis.model.Role_$$_javassist_6.toString(Role_$$_javassist_6.java)
    java.lang.String.valueOf(Unknown Source)
    java.lang.StringBuilder.append(Unknown Source)
    com.inhis.model.User.toString(User.java:97)
    java.lang.String.valueOf(Unknown Source)
    java.lang.StringBuilder.append(Unknown Source)
    java.util.AbstractCollection.toString(Unknown Source)
    org.hibernate.collection.PersistentBag.toString(PersistentBag.java:507)

Say, roles are

1, Unauthorized

2, Manager

3, User

What am I doing wrong? Also, I want assign them a default role of 1. I couldn't figure that out. So, I am storing it as null now. How can I achieve that?


You created an infinite loop. If you call the toString-method of a role it calls the toString-method for each user and this calls the toString of the role again. A possible solution is to remove this from the role-class:

"users=" + users +
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜