B
B
benzoback2017-03-17 10:37:42
Java
benzoback, 2017-03-17 10:37:42

@JsonIgnoreProperties for ArrayList how to do?

Hello!
There are two entities in my tutorial project: Animal and Client. This is an animal and a master. Each entity has a link to another, that is, Animal has a Client field, and vice versa. Entities are connected Many-to-One, an animal has only one owner, but one owner can have several animals.
The essence of the question is that I have a REST controller, and when I receive some kind of getClientById, I get a recursion where the client refers to the animal, and that, in turn, to the client, and so on in a circle.
I got around this with the @JsonIgnoreProperties annotation. But in the many-to-one case, everything works fine:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "client_id", insertable = false, updatable = false)
@JsonIgnoreProperties("animal")
private Client client;

and in the opposite case, all shit does not work perfectly:
@OneToMany(fetch = FetchType.LAZY, mappedBy = "client", cascade = CascadeType.ALL)
@JsonIgnoreProperties(value = { "client", "complaints" })
private List<Animal> animals;

and I even kind of know why: JsonIgnoreProperties looks at the fields of the List, instead of the Animal fields for each list item.
---
Maybe someone knows and tell me how to get around this? Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aol-nnov, 2017-03-17
@benzoback

it is necessary not to ignore, but to put @JsonBackReference and that's it. there will be no cyclic dependency

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question