如何解决hibernate双向映射引起的json序列化器中的循环引用? [英] How to solve circular reference in json serializer caused by hibernate bidirectional mapping?

查看:25
本文介绍了如何解决hibernate双向映射引起的json序列化器中的循环引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个序列化程序来将 POJO 序列化为 JSON,但遇到了循环引用问题.在休眠双向一对多关系中,父引用子引用和子引用回到父引用,这里我的序列化器死了.(参见下面的示例代码)
如何打破这个循环?我们可以获取一个对象的所有者树来查看对象本身是否存在于它自己的所有者层次结构中的某个地方吗?任何其他方法来查找引用是否将是循环的?或任何其他解决此问题的想法?

I am writing a serializer to serialize POJO to JSON but stuck in circular reference problem. In hibernate bidirectional one-to-many relation, parent references child and child references back to parent and here my serializer dies. (see example code below)
How to break this cycle? Can we get owner tree of an object to see whether object itself exists somewhere in its own owner hierarchy? Any other way to find if the reference is going to be circular? or any other idea to resolve this problem?

推荐答案

双向关系是否可以用 JSON 表示?某些数据格式不适合某些类型的数据建模.

Can a bi-directional relationship even be represented in JSON? Some data formats are not good fits for some types of data modelling.

在处理遍历对象图时处理循环的一种方法是跟踪您到目前为止看到的对象(使用身份比较),以防止自己遍历无限循环.

One method for dealing with cycles when dealing with traversing object graphs is to keep track of which objects you've seen so far (using identity comparisons), to prevent yourself from traversing down an infinite cycle.

这篇关于如何解决hibernate双向映射引起的json序列化器中的循环引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆