[英]Hibernate JPA bidirectional one-to-many results with constraint violation exception
[英]JPA/Hibernate bidirectional many-to-one results in StackOverflowException
我有实体User和GrantedRole具有双向一对多关系。
当我尝试将GrantedRole添加到Set in User时,没有抛出异常,但是当我调试User和GrantedRole对象的变量时,会有一个读取的描述
com.sun.jdi.InvocationException发生了调用方法。
调试时可以读取变量的不同字段,但是当我选择User中的角色字段或GrantedRole中的用户字段时,我得到与上面相同的描述。 当我进入用户的Set of GrantedRole时,我最终找到以下描述:
详细格式化程序错误:发生异常:java.lang.StackOverflowError
我的代码:
public class User {
private Set<GrantedRole> roles = new HashSet<GrantedRole>();
public User() {
super();
}
public User(String name, String password) {
this.name = name;
this.password = password;
}
@OneToMany(fetch = FetchType.EAGER, mappedBy = "user")
public Set<GrantedRole> getRoles() {
return roles;
}
public void setRoles(Set<GrantedRole> roles) {
this.roles = roles;
}
// equals and hashCode are based on username
// toString is based on all fields
}
public class GrantedRole {
private user user;
public GrantedRole() {
super();
}
public GrantedRole(User user, Role role, Organization organization) {
this.user = user;
this.role = role;
this.organization = organization;
}
@ManyToOne
@NotNull
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
// equals and hashCode are based on all fields
// toString was based on all fields, I've changed it to not include User.
}
public class Test {
public void testStuff() {
User user = new User("name");
GrantedRole role = new GrantedRole(user, "role"); //this sets the user field
user.getRoles().add(role); //doesn't throw Exception, but debugging shows InvocationTargetException and StackOverflowException
System.out.println(user.getRoles()); //throws StackOverflowException, as I would expect
}
}
我的理解是,我应该能够以这种方式建立双向关系。 我阅读了多个教程, 比如这个 ,我看不出我做的不同,导致.add(角色)出错。
我遗漏了一些琐碎的代码,如果需要,我很乐意提供它。 我没有制作代码来确保该用户引用具有对用户引用的GrantedRole作为回报,但我认为这与我遇到的问题无关。
所以,我只是愚蠢,并在toString()方法中进行了递归调用。 User.toString()尝试打印角色,GrantedRole.toString()尝试打印用户。
我通过改变GrantedRole.toString()来修复它,以打印user.getUsername(),从而打破循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.