Adding table joins to the existing project causes infinite recursion


I work on this project:

Here you can find all sources for entity etc. What I'm trying to do is to add a connection between TODO and USER. I want that to be a many-to-one relation since one user can have multiple todos, but todos can only have one owner.

I've created a join table in my MySQL database, with foreign keys etc.

Then I added following annotations:

And that is raising fatal error on Application start:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError) (through reference chain: sample.todosapp.spring.domain.User["todos"]->org.hibernate.collection.internal.PersistentSet[0]->sample.todosapp.spring.domain.Todo["user"]->sample.todosapp.spring.domain.User["todos"]->org.hibernate.collection.internal.PersistentSet[0]->sample.todosapp.spring.domain.Todo["user"]->sample.todosapp.spring.domain.User["todos"]->org.hibernate.collection.internal.PersistentSet[0]->sample.todosapp.spring.domain.Todo["user"]->sample.todosapp.spring.domain.User["todos"]->org.hibernate.collection.internal.PersistentSet[0]->sample.todosapp.spring.domain.Todo["user"]->sample.todosapp.spring.domain.User["todos"]


I'm completely lost. I search all related questions and tried all possible annotation variants, but still have no luch. What exactly I did wrong?

You should use mappedBy in one of your attributes and not define the relationship in both of them. Remove the annotation @JoinColumn from the HashSet Todo and change @OneToMany(cascade = CascadeType.ALL) to @OneToMany(mappedBy = "user" , cascade = CascadeType.ALL)