Json – How to solve circular reference in json serializer caused by Many TO Many hibernate bidirectional mapping

circular-referencehibernatejsonmany-to-manyspring

I am trying to serialize POJO to JSON but stuck in circular reference problem. I know how to handle one to many and reverse relationships using the @JsonBackReference and @JsonManagedReference.

My problem is with bidirectional many-to-many relation(eg a student can have many courses and each course can have many students enrolled), parent references child and child references back to parent and here my serializer dies. As per my understanding I cannot use @JsonBackReference here as value type of the property must be a bean: it can not be a Collection, Map, Array or enumeration.

Can some one please advise how I can handle this scenario?

Best Solution

You can use @JsonIgnoreProperties("someField") on one of the sides of the relations (the annotation is class-level). Or @JsonIgnore

Related Question