Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite recursion when using entities with a private identifier in a superclass, workaround #105

Open
cadbox1 opened this issue May 24, 2017 · 0 comments

Comments

@cadbox1
Copy link

cadbox1 commented May 24, 2017

@MappedSuperclass
class abstract Base {
    @Id
    private Integer id;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
}

@Entity
class A extends Base {
    @OneToMany
    private List<B> bs;

    public List<B> getBs() {
        return bs;
    }

    public void setBs(List<B> bs) {
        this.bs = bs;
    }
}

@Entity
class B extends Base {
    @ManyToOne
    @JoinColumn(name="a_id")
    private A a;

    public Integer getA() {
        return a;
    }

    public void setA(A a) {
        this.a = a;
    }
}

A and B have a bidirectional relationship and when they are both loaded and then it creates an infinite recursion and a stackoverflowerror. I started looking at the code and saw that it was using reflection but it occurred to me that making the superclass Id property protected instead of private might work like so.

@MappedSuperclass
class abstract Base {
    @Id
    protected Integer id;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
}

And it worked! I just wanted to share this in case anyone else had the same issue, i'm not sure if its worth a fix because using protected on the superclass seems fine to me but it did take me ages to work out what was wrong so maybe it is worth a fix. If you think it is worth fixing I can have a go at a PR.

Cheers,

Cadell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant