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?
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