6

I have a MySQL database and I want to retrieve some data as json.

And I have an entity Offre wich has @OneToMany relation with the AssociationCandidatOffre entity.

and I have an api which calles this method in my repository :

offreRepository.findAll();

Offre entity :

@Entity
public class Offre implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "CODE_OFFRE")
    private Long codeOffre;
    private String titre;

    @OneToMany(mappedBy = "offre")
    private Collection<AssociationCandidatOffre> associationCandidatOffres;

    public Collection<AssociationCandidatOffre> getAssociationCandidatOffres() {
        return associationCandidatOffres;
    }

    public void setAssociationCandidatOffres(Collection<AssociationCandidatOffre> associationCandidatOffres) {
        this.associationCandidatOffres = associationCandidatOffres;


    }
     //... getters/setters      
    }

AssociationCandidatOffre entity :

@Entity
public class AssociationCandidatOffre implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long idAssociation;
    private String lettreMotivation;
    private String tarifJournalier;
    private Date dateDisponibilite;

    @ManyToOne
    private Candidat candidat;

    @ManyToOne
    private Offre offre;
    @JsonIgnore
    @XmlTransient
    public Candidat getCandidat() {
        return candidat;
    }
    @JsonSetter
    public void setCandidat(Candidat candidat) {
        this.candidat = candidat;
    }
    @JsonIgnore
    @XmlTransient
    public Offre getOffre() {
        return offre;
    }
    @JsonSetter
    public void setOffre(Offre offre) {
        this.offre = offre;
    }

    //... getters/setters
}

the problem is when I call the api /offres to return me a json object I get this error message instead :

    Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: could not extract ResultSet (through reference chain: java.util.ArrayList[0]->com.***.Rekrute.entities.Offre["associationCandidatOffres"]); 
nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not extract ResultSet (through reference chain: java.util.ArrayList[0]->com.***.Rekrute.entities.Offre["associationCandidatOffres"])

when I use @JsonIgnore in the getAssocationCandidatOffres I dont get any errors but I want that association in the json result as well.

Normally, this shouldn't generate any error since I have @JsonIgnore in the other side of the relation which is getOffre().

how can I solve this problem ?

Renaud is Not Bill Gates
  • 1,684
  • 34
  • 105
  • 191
  • Are you sure the list `getAssocationCandidatOffres` is populated? Keep in mind that IDEs in debug mode will usually run a query to get any lazy loading list in background when you expand the list. – dambros Mar 31 '16 at 10:56

1 Answers1

2

You can't convert a bidirectional relation of an enitity to JSON. You get an endless loop.

JSON-Parser starts with the entity Offer and reads the associated AssociationCandidatOffre via getAssociationCandidatOffres(). For every AssociationCandidatOffre the JSON-Parser read getOffre() and starts again. The parser don't know when he must end.

Soumitri Pattnaik
  • 3,246
  • 4
  • 24
  • 42
UKoehler
  • 131
  • 7