Is there a way to influence the serialization process with @JsonIdentityInfo so that it inserts the whole object instead of referencing the id?
@Entity
@Json
-
The issue precisely is with how Jackson work serializing the java object to JSON.
When we use @JsonIdentityInfo to resolve the issue of cyclic dependency in object graph, we are forcing the serialization mechanism to serialize the first occurrence of Java object in JSON and the subsequent occurrence to be substituted by the id generated.
The determining factor when to create JSON for Java object is based on access made to the memory location where the JAVA Objects are created.
So, one good way to trick the serialization mechanism would be to create a copy of the child object and then set it. This way the same child object belonging to different parent in collection are treated as different child object instances.
One great way to create an exact copy of the object is using SerializationUtils.serialize/desrialize method.
byte[] bs= SerializationUtils.serialize(classObject);
ClassObject cloneEntity = (ClassObject ) SerializationUtils.deserialize(bs);
This creates an exact copy of the an object in different memory relocation.
- 热议问题