Reputation: 346
I'm trying to conditionally @JsonIgnore
some fields from an entity if they're serialized from an other entity's collection (many to one).
I've tried to add @JsonIgnoreProperties
to the collection but as I understand that annotation is not for this purpose.
class A {
//some fields
@ManyToOne private B b; //if only A is requested, this should NOT be ignored
}
class B {
//some fields
@OneToMany
@IgnorePrivateBInAToAvoidStackOverflow
private Set<A> collectionOfAs;
}
Is there any way to achieve this behavior?
Upvotes: 4
Views: 4662
Reputation: 1
That shoud work with @JsonIgnoreProperties
class A {
//some fields
@ManyToOne private B b; //if only A is requested, this should NOT be ignored
}
class B {
//some fields
@OneToMany
@JsonIgnoreProperties({'b', 'otherField'})
private Set<A> collectionOfAs;
}
Upvotes: 0
Reputation: 14698
Your usage of @ManyToOne
& @OneToMany
is incorrect, you must use @OneToMany
within the One
entity on the Many
entity collection property, and vice-versa for @ManyToOne
class A {
@ManyToOne
@JsonBackReference
private B b;
}
class B {
@OneToMany
@JsonManagedReference
private Set<A> collectionOfAs;
}
and as far as I can understand, you'd like to ignore the owner B
for doing a back reference from class A
, and create a stackoverflow exception, to achieve that use the annotation I've added in my above example @JsonBackReference
& @JsonManagedReference
which will stop the infinite loop on its tracks.
Upvotes: 3
Reputation: 14712
To avoid circular reference Infinite recursion ( stackoverflow error ) you have to annotate calss with @JsonIdentityInfo
So you class looks like :
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
class A {
//some fields
//Integer id;
@OneToMany private B b; //if only A is requested, this should NOT be ignored
}
Same thing for class B to bidirectional use :
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
class B {
//some fields
@ManyToOne
@IgnorePrivateBInAToAvoidStackOverflow
private Set<A> collectionOfAs;
}
be aware that the property
refers to your unique field name ( set to id
in this example )
for more reading see this article
Upvotes: 3
Reputation: 124
If Class B has a collection of Class A then the Set of A in the Class B should be annotated @OneToMany
, and the field in class A should be annotated @ManyToOne
, then you can put your @JsonIgnore
on the set like so:
class A {
//some fields
@ManyToOne private B b; //if only A is requested, this should NOT be ignored
}
class B {
//some fields
@OneToMany
@JsonIgnore
private Set<A> collectionOfAs;
}
My guess is that you were receiving StackOverflow error because when you were fetching some object of Class B, it brought the Set of objects of Class As with it, which themselves brought the same object of Class B which was originally fetched, and this would go infinitely, unless you provided @JsonIgnore on the Set field. This way, when you call an object of Class A, their field object of Class B will also be fetched, but when you call an object of Class B, their collection of Class As will be ignored.
Upvotes: 0