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?
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"})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With