6

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.

Emerson
  • 428
  • 5
  • 16
  • 3
    You can partly solve this using `@JsonbTransient` on the field that you want to exclude, however this won't set the backreference during deserialization - which is a really neat feature of Jackson. – Jan-Willem Gmelig Meyling May 29 '20 at 12:57

1 Answers1

6

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

Anuj Vishwakarma
  • 812
  • 8
  • 22