JPA/Hibernate双向多对一导致StackOverflowException

Joh*_*eke 8 java hibernate jpa bidirectional one-to-many

我有实体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
    }
}
Run Code Online (Sandbox Code Playgroud)

我的理解是,我应该能够以这种方式建立双向关系.我阅读了多个教程,比如这个,我看不出我做的不同,导致.add(角色)出错.

我遗漏了一些琐碎的代码,如果需要,我很乐意提供它.我没有制作代码来确保该用户引用具有对用户引用的GrantedRole作为回报,但我认为这与我遇到的问题无关.

Joh*_*eke 18

所以,我只是愚蠢,并在toString()方法中进行了递归调用.User.toString()尝试打印角色,GrantedRole.toString()尝试打印用户.

我通过改变GrantedRole.toString()来修复它,以打印user.getUsername(),从而打破循环.

  • 使用.hashCode()方法可能会发生同样的情况:) (2认同)