当我有两个班在一个双向多对多的关系如下图所示:失去孩子的数据在双向关系使用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