使用jacksonMapper时,以双向关系丢失子数据 [英] Losing the child data in a bi-directional relationship when using jacksonMapper
问题描述
我有两个类,如下所示,双向多对多关系:
I have two classes as shown below in a bi-directional Many to Many relationship:
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得到一个特定的父母。与父详细信息一起,我想获取父项的子项。这样的事情:
I make a call in my DAO to get a particular parent. Alongwith the parent details I want to fetch the children of the parent. Something like this:
Hibernate.initialize(parent.getChildSet()); //this works perfectly
// and I get the details of parent along with the children in my DAO call.
但是当我在业务服务中执行以下操作时将数据返回给控制器时,子项被省略来自父json字符串。
But when I do the below in my business service while returning the data to the controller the children are omitted from the parent json string.
jacksonMapper.writeValueAsString(parent);
所以我在Parent类中移除了@JsonIgnore上的Child属性,认为jackson可能会理解这些字段是写入字符串时不要忽略,如下所示。但它仍然忽略了它们! :(
So i removed the @JsonIgnore on Child attribute inside Parent class thinking that jackson might understand that these fields are not to be ignored while writing to a string as shown below. But it still does ignore them! :(
Parent implements Serializable{
@ManytoMany(//declaration for join table)
@JsonBackReference
//@com.fasterxml.jackson.annotation.JsonIgnore
Set <Child> childSet;
}
我知道哪里可能出错?
推荐答案
我一直无法找到原因。同时我选择了一种解决方法。我正在对DB进行两次单独的调用。一次是先获取父级,然后是第二次根据获取的内容获取子级。 parentId。
I have not been able to find out why this is happening. Meanwhile I have opted for a workaround. I am making two separate calls to DB. One to fetch the parent first and then second to fetch the child based on the fetched parentId.
或者,我可以同时在服务上同时进行数据库调用,并在将JSON发送到ui之前将其作为复杂字符串进行准备:
Alternatively, I can make both the DB calls at the service same time and prepare the JSON as a complex string before sending it to the ui:
complex:{
parent:parent,
child:child
}
在任何一种情况下,它都是一种解决方法。理想的解决方案是仅在父映射中删除@JsonIgnore儿童班的一面。但不知何故,这似乎不起作用。如果我发现为什么理想的解决方案无效,我会发帖!
In either case, it is a workaround. The ideal solution is just remove@JsonIgnore in the mapping only from the parent side for the child class. But somehow that doesn't seem to work. I'll post in case I find why the "ideal" solution is not working!
理想解决方案2016年8月15日更新为答案印度独立日:
问题在于映射:
Parent implements Serializable{
@ManytoMany(//declaration for join table)
@JsonBackReference
@com.fasterxml.jackson.annotation.JsonIgnore
Set <Child> childSet;
}
当你说@JsonBackReference它实际上意味着忽略这个字段而写Json,也就是说,
When you say @JsonBackReference it actually means ignore this field while writing the Json, i.e. to say,
@JsonBackReference< - > @JsonIgnore
@JsonBackReference <-> @JsonIgnore
因此,在序列化父项时,将省略子项。使用ORM映射,将注释设置为单面而非双面是最佳实践。通过这种方式,您可以在获取数据时避免大量不必要的异常,其次,保持业务代码清洁。
Hence the child is omitted when the parent is serialized. With ORM mappings it's always a best practice to have the annotations one sided rather than double sided. In that way, you can avoid a lot of unwanted exceptions while fetching the data and secondly, keep your business code clean.
JsonManagedReference vs JsonBackReference
这篇关于使用jacksonMapper时,以双向关系丢失子数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!