1

I have the following entities in my Spring boot Hibernate project.

Entities

  1. Task - represents the task / job details
  2. Agent - represents the agents available to work on the tasks
  3. TaskAgent - stores the details of agents who are assigned to work on a task

Relations

  1. Task has a ManyToOne reference to TaskAgent with name taskAgents
  2. Agent also has a ManyToMany reference to TaskAgent with name agentTasks.
  3. 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

  1. /tasks (GET) - returns the list of all the tasks and if the task has been assigned to an Agent, send the agent details too

  2. /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

1 Answer 1

1

For the benefit of others, I solved this using

@JsonIdentityInfo(generator= JSOGGenerator.class)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.