Jackson - serialization of entities with birectional relationships (avoiding cycles)
Solution 1
Jackson 2.0 does support full cyclic object references. See "Jackson 2.0 released" (section 'Handle Any Object Graphs, even Cyclic ones!') for an example.
Basically, you will need to use new @JsonIdentityInfo
for types that require id/idref style handling. In your case this would be both Parent
and Child
types (if one extends the other, just add it to super type and that's fine).
Solution 2
very handy interface implementation is provided in jackson 2 library as
@Entity
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Parent { ....
@Entity
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Child { ....
in maven
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.0.2</version>
</dependency>
@StaxMan provided a nice link to start from
Eugene Retunsky
Updated on July 09, 2022Comments
-
Eugene Retunsky almost 2 years
I have two entities:
Parent { Child[] children; } and Child { Parent parent; }
I'm aware about
@JsonBackReference
and@JsonManagedReference
. They are good, if I'm serializing instances ofParent
.But I also need to transfer instances of
Child
and I want to have theparent
field populated.In other words:
- On serialization of
Parent
it should havechildren
but their parent field might be empty (can be solved by using json reference annotations). - On serialization of
Child
it should haveparent
with theirchildren
(butchildren
don't have to haveparent
populated.
Is there a way to solve it using standard Jackson capabilities?
I.e. skip serialization of entities which were already serialized instead of marking fields eligible or non-eligible for serialization.
- On serialization of
-
Eugene Retunsky about 12 yearsPretty fresh release. Requires some package moving. OK, I'll check it out.
-
StaxMan about 12 yearsYeah, it is, big upgrade. But feature itself is big too. :-)
-
f.khantsis about 12 yearsHow do you deserialize using JSON2 in JavaScript?
-
StaxMan about 12 yearsSorry -- Jackson 2.0 is just version of library; result is just regular JSON. But as to how to resolve references, for that you would need a matching javascript library: JSON format does not have anything to explicitly help in resolving deps.
-
Mab over 8 yearsfurther supportive reading wiki.fasterxml.com/JacksonFeatureObjectIdentity
-
raikumardipak about 8 yearsI have a similar problem but with no luck after trying this solution :stackoverflow.com/q/36809325/3300911
-
raikumardipak about 8 yearsAny idea plz on eat cud b the problem
-
Oleg Abrazhaev almost 8 yearsdoesn't helped with hibernate mapping
-
Oleg Abrazhaev almost 8 yearsdoesn't help with hibernate mapping
-
Oleksii Kyslytsyn over 7 yearsThis link describes with few more details: github.com/FasterXML/jackson-docs/wiki/Presentation-Jackson-2.0
-
Krish over 6 years@StaxMan it will create IDs if the pojo already exists. How to avoid it ? I have created one issue regarding that. can you please help ?stackoverflow.com/questions/47078057/…
-
Carsten Flokstra over 4 yearsYou rock! Searched the internet for this problem. Finally found a solution. Big thanks!
-
Harsh about 4 yearsThis solution worked perfectly for me for Spring Data MongoDB DBRef Two way referencing. Thanks!!
-
fernandopcg about 2 yearsThis is a solid answer, just want to add to be careful if using Lombok! Add "@EqualsAndHashCode(exclude = {"fieldInRelationship"})" to both classes or those annotations will be ignored as was happening in my case!