I have the following entities in my Spring boot Hibernate project.
Entities
- Task - represents the task / job details
- Agent - represents the agents available to work on the tasks
- TaskAgent - stores the details of agents who are assigned to work on a task
Relations
- Task has a ManyToOne reference to TaskAgent with name taskAgents
- Agent also has a ManyToMany reference to TaskAgent with name agentTasks.
- However TaskAgent has a ManyToOne reference to Task with name task and ManyToOne reference to Agent with name selectedAgent.
I also have the following API endpoints in my controllers
End Points
/tasks (GET) - returns the list of all the tasks and if the task has been assigned to an Agent, send the agent details too
/agents (GET) - returns the list of all the agents and also include the list of all the assigned tasks for the agents
Problem
This setup is unfortunately resulting in infinite recursion. I know what the problem is
Fetching Task results in populating taskAgents(of type TaskAgent where the details of who is assigned to work on that task is stored) which contains selectedAgent which contains a ManyToMany reference to TaskAgents again( where the list of all assigned tasks for that agent is available)
I tried LAZY_LOAD with Hibernate5Module, @JsonBackReference and @JsonManagedReference, @JsonIgnore, @JsonIgnoreProperties in parent entity etc.
How can I make both these endpoints work without recursion?
Thanks