我正在尝试使用hibernate和struts2构建一个登录页面。我的设计如下。每个登录用户都有一个角色。许多用户可以扮演相同的角色
所以我的班级是:
的 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 + '}';
}
}
如果有效,它会返回1个用户。但是由于该方法返回该用户,我使用return users.get(0);
堆栈
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)
说,角色是
1,未经授权
2,经理
3,用户
我究竟做错了什么?另外,我想为它们分配默认角色1.我无法弄明白。所以,我现在将它存储为null。我怎样才能做到这一点?
答案 0 :(得分:0)
您创建了一个无限循环。 如果调用角色的toString方法,它会为每个用户调用toString方法,并再次调用该角色的toString。 一种可能的解决方案是从角色类中删除它:
"users=" + users +