2017-04-04 75 views
1

我在一個類中有多個反向引用類。由於我爲他們使用了@JsonBackReference,所以出現錯誤。我爲這些類分配了@JsonIdentityInfo註釋,但我仍然得到相同的錯誤。名稱爲'defaultReference'的多個反向引用屬性

public class X implements Serializable { 
    .... 
    //bi-directional many-to-one association to Booking 
    @ManyToOne(fetch = FetchType.EAGER) 
    @JoinColumn(name = "xxA", nullable = false) 
    @JsonBackReference 
    private A a; 

    //bi-directional many-to-one association to Client 
    @ManyToOne(fetch = FetchType.EAGER) 
    @JoinColumn(name = "xxB", nullable = false) 
    @JsonBackReference 
    private B b; 
    ...getters setters 
} 

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id") 
public class B implements Serializable { 
    ........ 
    //bi-directional many-to-one association to BookedClient 
    @OneToMany(mappedBy = "b", fetch = FetchType.EAGER, cascade = CascadeType.ALL) 
    @JsonManagedReference 
    private List <X> xxB; 
    ........ getters setters 
} 


@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id") 
public class A implements Serializable { 
    ........ 
    //bi-directional many-to-one association to BookedClient 
    @OneToMany(mappedBy = "a", fetch = FetchType.EAGER, cascade = CascadeType.ALL) 
    @JsonManagedReference 
    private List <X> xxA; 
    ........ getters setters 
} 

錯誤:

com.fasterxml.jackson.databind.JsonMappingException: Multiple back-reference properties with name 'defaultReference'

我怎樣才能解決這個問題?我不能在課堂上使用多個後臺引用嗎?

回答

1

Jackson's javadoc,既@JsonManagedReference@JsonBackReference接受一個名稱值結合在一起:

@JsonBackReference("a") 
    private A a; 

    @JsonManagedReference("a") 
    private List <X> xxA; 
+0

我爲每個關係添加了一個名稱值。我仍然得到同樣的錯誤。 – Eniss

+0

關於名稱'defaultReference'的同樣錯誤?奇怪 –

+0

是的,我也試過'@JsonManagedReference(value =「a」)'''@JsonBackReference(value =「a」)',仍然是同樣的錯誤 – Eniss

0

我也面臨這個問題,但在最後我解決它。

//This is parent class 
@Entity 
@Table(name = "checklist") 
@JsonIgnoreProperties("inspection") 
public class Checklist implements java.io.Serializable { 

    @ManyToOne 
    @JoinColumn(name = "product_id", referencedColumnName = "id") 
    @JsonBackReference 
    private Product product; 

    @OneToMany(mappedBy = "checklists", cascade = CascadeType.ALL) 
    @JsonManagedReference 
    private Set<Inspection> inspection = new HashSet<Inspection>(); 
//Constructor 
//Getter and Setter 
} 

//This is child class 
@Entity 
@Table(name = "inspections") 
public class Inspection { 

    @ManyToOne 
    @JoinColumn(name = "chk_id", referencedColumnName = "id") 
    private Checklist checklists; 
//Constructor 
//Getter and Setter 
} 

通過在父類

解決了這個問題提@JsonIgnoreProperties("inspection")@JsonManagedReference在同一個父類中使用兩個@JSONBackRefrence提高。