Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How solve bidirectional relationships on JSONB serialization?

I'm trying to serialize an entity with have a bidirectional relationship:

class TypeA {
  String name;
  TypeB typeB; 
}

class TypeB {
  String identifier;
  TypeA typeA;
}

With Jackson I solve with @JsonBackReference in typeB attribute and @JsonManagedReference in typeA attribute, but how i can do this on JSONB (Eclipse Yasson implementation)?

Caused by: javax.json.bind.JsonbException: Recursive reference has been found in class class xxxxxx.model.Analysis.
        at org.eclipse.yasson.internal.serializer.ObjectSerializer.serializeInternal(ObjectSerializer.java:76)
        at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:107)
        at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:125)
        at org.eclipse.yasson.internal.serializer.ObjectSerializer.marshallProperty(ObjectSerializer.java:121)
        at org.eclipse.yasson.internal.serializer.ObjectSerializer.serializeInternal(ObjectSerializer.java:69)
        ... 45 more

OBS: I solved with a DTO but the doubt stayed.

like image 821
Emerson Avatar asked May 20 '20 14:05

Emerson


1 Answers

For solving the circular structure you should use @JsonbTransient. As stated in documentation [0]:

  1. By default, JSONB ignores properties with a non public access. All public properties - either public fields or non public fields with public getters are serialized into JSON text.
  2. Class properties annotated with @JsonbTransient annotation are ignored by JSON Binding engine.

[0]: http://json-b.net/docs/user-guide.html#ignoring-properties

like image 154
Anuj Vishwakarma Avatar answered Nov 14 '22 05:11

Anuj Vishwakarma