JAX WS RS JPA does not return oneToMany relation in JSON

-1

In my relationship, a Call is related to many Histories. The key of the Call is present in every History. When querying a History for the code, there is a @ManyToOne relation that returns the Call to which the history belongs. When consulting the Call, the answer should contain the Consulted Call and all the Histories related to that Call. This relationship is mapped in the called model as @OneToMany.

It is possible to instantiate in the class the Historical Collection and generate an output in the console, however the history is not sent in JSON to the client.

Note: All JPA classes were automatically generated from NetBeans, using the "New > Database Entity Class ..." option.

    
asked by anonymous 10.07.2018 / 03:05

1 answer

0

After much research, I came up with the next solution.

The Calling template must contain in the Historical Collection attribute the annotation that informs the relationship type and as a parameter the load type: LAZY or EAGER must be passed. In my case the EAGER was necessary to always bring history.

@OneToMany(cascade = CascadeType.ALL, mappedBy = "chamado", fetch = FetchType.EAGER)
@JsonManagedReference
private Collection<Historico> historicoCollection;

In the History class the mapping stays that way.

@JoinColumn(name = "chamado", referencedColumnName = "codigo")
@ManyToOne(optional = false)
@JsonBackReference
private Chamado chamado;

I also had to exclude the @XmlTransient annotation.

//@XmlTransient
public Collection<Historico> getHistoricoCollection() {
    return historicoCollection;
}

As shown in the following question:

link

After deleting the @XmlTransiente annotation in the annotation, a recursion error is generated. For this, the solution to the problem is explained , which is just to inform who is the class that contains the relation, through the annotations @JsonBackReference and @JsonManagedReference.

    
10.07.2018 / 04:34