Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@OneToMany stackoverflow in Spring Data Rest

I have a Questions entity which has list of options as follows:

@OneToMany(mappedBy = "question")
List<Option> options;

And in Options entity I have specified the relation as :

@ManyToOne
@JoinColumn(name="question_id")
Question question;

When I hit /api/questions , it works fine but when I hit /api/questions/1 , it gives java.lang.StackOverflowError: null

What am I doing wrong?

like image 942
ArslanAnjum Avatar asked Feb 18 '26 10:02

ArslanAnjum


1 Answers

It's because Option refers to Question and Question to Option. You should add @JsonIgnore to one of your class to prevent infinite linking to each other. The same thing can be with toString() method. If you use Lombok or generate default toString method, it could cause statckoverflow also. Because classs linked to class. To prevent this try to exclude link on class in one of toString method. In Lombok in @ToString annotation add exclude statement and exclude either Option or Question. Maybe you call toString method that cases loop. @ToString(exclude = {"option"})

like image 73
Den B Avatar answered Feb 21 '26 15:02

Den B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!