I have two classes which has bidirectional relationship and having recursive object structure issue due to the way it is being set.
Employer employer = new Employer();
employer.setName("sample1");
Employee empl = new Employee();
List<Employer> listOfEmployers = new ArrayList<Employer>();
listOfEmployers.add(employer); //i am assuming this part of code is causing recursion issue
empl.setEmployers(listOfEmployers);
employer.setEmployee(empl); //will have recursive object structure
It causes issue while conversion of java object to JSON (org.codehaus.jackson.map.JsonMappingException: Infinite recursion). Because of this, jackson is not able to process the java object into JSON.
However,Can you please help me understand how exactly its happening behind the scenes in Java to fix the root-cause?
And what is the right object model to form this kind of mapping between Employer and Employee without getting this recursion issue in java layer?