JSON: Circular Dependency Errors
If you’re using Object Relational Mapping frameworks like Hibernate, and are using the bi-directional mappings then you can be sure of getting the following errors if you try to generate JSON of the entities involved.
1. If you’re using Jackson
|
1
|
com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) |
2. If you’re using Google’s Gson
|
1
|
java.lang.IllegalStateException: circular reference error |
Let’s take a case of two entities Actor, and Movie having a bi-directional relationship and using them we’ll see how to get over the above exceptions when we try to generate JSON.
This solution uses Jackson for generating JSON.
So if you’re using Google’s Gson you’ll have to write an exclusion strategy yourself, and more importantly the rest of the post is useless for you, unless you’re willing to switch to Jackson like I did with such great finesse! ;)
If yes, then you can download Jackson’s libraries and continue reading the post further, rest assured that it’ll be of immense help to you! :)
Now, these entities will have a many-to-many relationship amongst them. As one Actor can act in many Movies and one Movie can have many Actors (as it so often happens, and by the way due apologies for being trite and rhetoric!).
Actor.java
|
1
2
3
4
5
6
7
8
9
10
|
@Entity@Table(name="Actor")public class Actor implements Serializable { . . . /* This is the exception-saving-annotation */ @JsonManagedReference @ManyToMany private List<Movie> movies; . . .} |
Movie.java
|
1
2
3
4
5
6
7
8
9
10
|
@Entity@Table(name="Movie")public class Movie implements Serializable { . . . /* This is the exception-saving-annotation */ @JsonBackReference @ManyToMany(mappedBy="movies") private List<Actor> actors; . . .} |
So all we did was adding two annotations to the fields having a relationship.
- @JsonManagedReference: This annotation should be put on the owner of the relationship, or as you may deem fit.
- @JsonBackReference: This annotation should be used on the other end of the relationship.
Now, having done that it’s extremely easy to generate the JSON.
|
1
2
3
4
5
6
|
//com.fasterxml.jackson.databind.ObjectMapperObjectMapper objectMapper = new ObjectMapper();//The input argument of the writeValueAsString() function can be a bean, array, list, map or a set.String actorsAsJson = objectMapper.writeValueAsString(actorsList);//actorsList is a variable of type List<Actor>//Voila! You're done! |
That’s all for the circular dependency errors.
source :https://ankeetmaini.wordpress.com/2012/07/26/json-troubleshooting-circular-dependency-errors/
JSON: Circular Dependency Errors的更多相关文章
- 在.NET Core中遭遇循环依赖问题"A circular dependency was detected"
今天在将一个项目迁移至ASP.NET Core的过程中遭遇一个循环依赖问题,错误信息如下: A circular dependency was detected for the service of ...
- Circular dependency issuse on cocoapods version(0.36.0) 全然解决方式(非降版本号)
此前的文章中.以前提到在cocoapods依赖管理版本号假设超过0.34.4.比方0.35, 0.36.0等版本号中. 运行"pod install" 或者 "pod u ...
- DAX的圈圈大坑:循环依赖关系错误circular dependency (单表篇)
使用DAX中的某些函数特别类似Calculate这种函数创建计算列时很容易出现一种错误,叫做检测到循环依赖关系,即:A circular dependency was detected.对于刚接触Da ...
- Intellij Error:Cannot build Artifact 'XXX:war exploded' because it is included into a circular dependency
外网的流程是这样的. 1: 2: 3: 4: 基本按这个来就好了 如果到了build artfact哪里按钮是灰色 就要手动建了 https://jingyan.baidu.com/album/0a5 ...
- Error:Cannot build artifact 'ssm:war exploded' because it is included into a circular dependency (artifact 'ssm:war exploded', artifact 'apinb-master:war exploded')
打开 File->Project Structure –> Artifacts(ctrl+alt+shift+s) ,这里会有4个,我已经删除了,把 ssm:war 和 ssm:war e ...
- Error:Cannot build artifact 'XXX:war exploded' because it is included into a circular dependency (artifact 'XXXX:war exploded', artifact 'XXX:war exploded') Idea启动项目报错解决方案
在Idea中使用Maven创建父子工程,第一个Model的那个项目可以很好的运行,在创建一个Model运行时报这个错.原因是tomcat部署了多个Web项目,可能最开始是两个项目的配置文件混用用,最后 ...
- 在Windows平台下Qt的exe报错问题排查步骤
在Windows平台下Qt的exe报错问题排查步骤 工具介绍: 1. Dependency Worker Dependency Worker是一个免费的用具用来扫描任何的32bit 或者64bit 的 ...
- Linux和windows 查看程序、进程的依赖库的方法
Linux: 1. 利用ldd查看可执行程序的依赖库 [root@~]# ldd /usr/local/php/bin/php linux-vdso.so.1 => (0x00007ff ...
- Some_tools
Why and what There are lots of nice or great tools on the internet, sometimes I will just forget a p ...
随机推荐
- 生成优惠券,并将优惠券存入Mysql
#coding:utf-8 import random import string import MySQLdb def gen_charint(filename, width =4, num=5): ...
- javascript、jquery 、C#、sqlserveer、mysql、oracle中字符串截取的区别和用法
下标从0开始 ,并且包括起始位 javascript 中字符串截取 : substring(Number start,Number end) var substr = "liuguangfa ...
- [luoguP2736] “破锣摇滚”乐队 Raucous Rockers(DP)
传送门 f[i][j]表示前i首歌放到前j个盘里最多能放多首 ntr[i][j]表示i~j中最多能放进一张盘中多少首歌 ntr数组可以贪心预处理出来. #include <cstdio> ...
- hud 2073
#include<stdio.h> #include<math.h> int main() { int i,j,k,n,m,t; double a]; a; for;i++) ...
- @Temporal()注解的使用
数据库的字段类型有date.time.datetime而Temporal注解的作用就是帮Java的Date类型进行格式化,一共有三种注解值: 第一种:@Temporal(TemporalType.DA ...
- 介绍 JSON的
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于机器解析和生成. 它基于JavaScript Programming Lan ...
- 修改textfield的占位符颜色
[self.titleField setValue:UIColorFromHEXWithAlpha(0x999999, 1) forKeyPath:@"_placeholderLabel.t ...
- codeforces 301 E. Infinite Inversions
题目: time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...
- [CERC2015]Digit Division
题目描述 We are given a sequence of n decimal digits. The sequence needs to be partitioned into one or m ...
- Java后端技术书单
写博客记录技术上使用的各种问题,这个只能算是一个打游击. 如果要把一个知识学透,最有效的方式就是系统学习,而系统学习就是看书,书本上有清晰的学习路线以及相应的技术栈. 下面是我收集的Java后端的技术 ...