我在双向多对多关系中有两个类,如下所示:
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 on Child 属性,认为杰克逊可能会理解在写入字符串时不会忽略这些字段,如下所示。但它仍然忽略它们!:(
Parent implements Serializable{
@ManytoMany(//declaration for join table)
@JsonBackReference
//@com.fasterxml.jackson.annotation.JsonIgnore
Set <Child> childSet;
}
知道我哪里可能出错了吗?