2016-03-22 14 views
6

私はMySQLデータベースを持っており、jsonとしていくつかのデータを取得したいと思います。春のブート時にjsonとしてデータベースからデータを取得する

そして、AssociationCandidatOffreエンティティと@OneToManyの関係を持つエンティティOffreがあります。

と私は私のリポジトリにこの方法をcalles APIがあります。

offreRepository.findAll(); 

Offreをエンティティ:

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

問題がありますapi /offres私にjsonオブジェクトを返すにはこのエラーメッセージが出ます広告:私はgetAssocationCandidatOffres@JsonIgnoreを使用する場合

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

私はすべてのエラーを取得いけないが、私は同様にJSONの結果でその関連付けをしたいです。

getOffre()という関係の反対側に@JsonIgnoreがあるので、通常これはエラーを生成しません。

どうすればこの問題を解決できますか?

+0

'getAssocationCandidatOffres'リストに値が設定されていますか?デバッグモードのIDEは通常、リストを展開するときに遅延ロードリストをバックグラウンドで取得するためにクエリを実行します。 – dambros

答えて

2

エンティティの双方向関係をJSONに変換することはできません。 無限ループが発生します。

JSON-Parserは、Offerというエンティティで始まり、をgetAssociationCandidatOffres()経由で読み取ります。 AssociationCandidatOffreごとに、JSON-ParserがgetOffre()と読み込み、もう一度開始します。パーサーはいつ終わらなければならないのか分からない。

関連する問題