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

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

问题描述

我正在编写一个序列化程序来将POJO序列化为JSON,但陷入了循环引用问题。在hibernate双向一对多关系中,父级引用子对象和子对象引用回父对象,在这里我的序列化程序会死亡。 (请参阅下面的示例代码)

如何打破这个循环?我们能否获得对象的所有者树来查看对象本身是否存在于它自己的所有者层次结构中的某个位置?任何其他方式来查找参考是否将是循环?或任何其他想法来解决这个问题?

解决方案

双向关系甚至可以用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?

解决方案

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.

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

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