當我有兩個班在一個雙向多對多的關係如下圖所示:失去孩子的數據在雙向關係使用jacksonMapper
Parent implements Serializable{
@ManytoMany(//declaration for join table)
@JsonBackReference
@com.fasterxml.jackson.annotation.JsonIgnore
Set <Child> childSet;
}
Child implements Serializable{
@ManytoMany(//declaration for join table)
@JsonManagedReference
@com.fasterxml.jackson.annotation.JsonIgnore
Set <Parent> parentSet;
// other getter and setters
}
我讓我的DAO調用得到特定的父母。隨着父母的細節,我想要取得父母的孩子。事情是這樣的:
Hibernate.initialize(parent.getChildSet()); //this works perfectly
// and I get the details of parent along with the children in my DAO call.
但是當我做了以下我的業務服務,同時將數據返回到控制器的孩子從父JSON字符串省略。
jacksonMapper.writeValueAsString(parent);
所以我刪除裏面父類的思維,傑克遜可能會明白,這些領域是不可忽視的,同時,如下圖所示寫入字符串子屬性@JsonIgnore。但它仍然會忽略它們! :(
Parent implements Serializable{
@ManytoMany(//declaration for join table)
@JsonBackReference
//@com.fasterxml.jackson.annotation.JsonIgnore
Set <Child> childSet;
}
任何想法,我可能會去錯了嗎?
你可以添加你的數據庫查詢嗎? – k1133
@ k1133數據庫端我使用休眠,它是一個選擇FoodItem上的所有查詢。我想要的類別也與foodItem一起來。但在使用jacksonMapper時,這些類別在返回的json字符串中被遺漏。 – dirai
我已經刪除了Parent類中的Child屬性上的@JsonIgnore,他認爲Jackson可能會理解這些字段在寫入字符串時不會被忽略。但它仍然會忽略它們!:( – dirai