JPA / Hibernate双向多对一导致StackOverflowException [英] JPA/Hibernate bidirectional many-to-one results in StackOverflowException

查看:477
本文介绍了JPA / Hibernate双向多对一导致StackOverflowException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有实体User和GrantedRole,它们具有双向的一对多关系。

I have entities User and GrantedRole that have a bidirectional one-to-many relation.

当我尝试将GrantedRole添加到Set in User时,有没有异常抛出,但是当我为User调试变量时,GrantedRole对象的描述为

When I try to add a GrantedRole to the Set in User, there is no exception thrown, but when I debug the variables for the User and GrantedRole object have a description that reads


com.sun.jdi.InvocationException发生了调用方法。

com.sun.jdi.InvocationException occurred invoking method.

调试时可以读取变量的不同字段,但是当我选择User中的角色字段时GrantedRole中的用户字段我得到与上面相同的描述。
当我进入用户的Set of GrantedRole时,我最终找到以下描述:

The different fields for the variables can be read while debugging, but when I select the roles field in User or the user field in GrantedRole I get the same description as above. When I go into the Set of GrantedRole in user, I eventually find the following description:


详细格式化程序错误:
发生异常:java.lang.StackOverflowError

Detail formatter error: An exception occurred: 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(角色)出错。

It was my understanding that I should be able to set up a bidirectional relation in this way. I read multiple tutorials, like this one, and I don´t see what I am doing differently to cause the .add(role) to go wrong.

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

I left out some trivial code, if it is needed I will gladly provide it. I haven't made code to ensure that a GrantedRole with a reference to a User is referenced by that user in return, but I think that is not relevant for the problem I'm having.

推荐答案

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

So, I was just being stupid and had a recursive call in the toString() methods. User.toString() tried to print the roles, and GrantedRole.toString() tried to print the user.

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

I fixed it by altering the GrantedRole.toString() to print user.getUsername(), thus breaking the cycle.

这篇关于JPA / Hibernate双向多对一导致StackOverflowException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆