Reputation: 151
CONTEXT:
I have 2 webapps:
both communicate by webservice. I'm trying to generate an objet (CounterDTO) in webservice webapp from json like this :
ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); CounterDTO counterDTO = mapper.readValue(json.get(objName).toString(), CounterDTO.class);
ERROR:
When I try to build an objet with a json, I have this error:
java.lang.IllegalArgumentException: Can not handle managed/back reference 'entreprise-agency': back reference type (java.util.Set) not compatible with managed type (com.mypackage.Agency)
JSON sent:
{"id":null,"code":"SKYUE586","name":"name 1"}
I have 3 entities like this :
@Entity
@Table(name = "AGENCY")
public class Agency {
...
@ManyToOne(optional = false)
@JoinColumn(name = "ID_Entreprise")
@JsonManagedReference(value = "entreprise-agency")
private Entreprise entreprise;
@OneToMany(mappedBy = "agency", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JsonBackReference(value = "agency-counter")
private Set<Counter> counters = new HashSet<Counter>();
...
}
.
Entity
@Table(name = "COUNTER")
public class Counter {
...
@ManyToOne(optional = false)
@JoinColumn(name = "ID_Agence")
// @JsonBackReference
@JsonManagedReference(value = "agency-counter")
private Agency agency;
...
}
.
@Entity
@Table(name = "ENTREPRISE")
public class Entreprise {
...
@OneToMany(mappedBy = "entreprise", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JsonBackReference(value = "entreprise-agency")
private Set<Agency> agencies = new HashSet<Agency>();
...
}
and DTO
public class CounterDTO {
private Integer id;
private String code;
private String name;
...
}
QUESTION:
How can I properly handle managed/back jackson reference ?
I've also tried to use this tag on my entities
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
Thanks
Upvotes: 0
Views: 595