2016-03-22 14 views
6

Mam bazę danych MySQL i chcę pobrać niektóre dane jako json.pobieranie danych z bazy danych jako json w rozruchu wiosennym

I mam jednostkę Offre, która ma relację @OneToMany z jednostką AssociationCandidatOffre.

i mam api które Calles tę metodę w moim repozytorium:

offreRepository.findAll(); 

OFFRE podmiot:

@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  
    } 

jednostka AssociationCandidatOffre:

@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 
} 

problem jest gdy zgłoszę api /offres do zwrócenia mi obiektu json. Otrzymuję komunikat o błędzie inste ad:

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"]) 

kiedy używam @JsonIgnore w getAssocationCandidatOffres ja nie otrzymuję żadnych błędów, ale chcę to stowarzyszenie w wyniku json, jak również.

Zwykle nie powinno to generować żadnego błędu, ponieważ mam @JsonIgnore po drugiej stronie relacji, która jest getOffre().

Jak mogę rozwiązać ten problem?

+0

Czy jesteś pewien, że lista 'getAssocationCandidatOffres' jest wypełniona? Należy pamiętać, że IDE w trybie debugowania zwykle uruchamiają zapytanie, aby uzyskać listę leniwych załadowań w tle po rozwinięciu listy. – dambros

Odpowiedz

2

Nie można przekonwertować dwukierunkowej relacji jednostki do JSON. Otrzymujesz nieskończoną pętlę.

JSON-Parser rozpoczyna się od jednostki Offer i odczytuje powiązaną AssociationCandidatOffre przez getAssociationCandidatOffres(). Dla każdego AssociationCandidatOffre JSON-Parser czyta getOffre() i rozpoczyna się od nowa. Parser nie wie, kiedy musi się skończyć.

Powiązane problemy