springboot 使用hibernate进行双向关联json解析时死循环处理
来源:互联网 发布:百度贴吧查ip软件 编辑:程序博客网 时间:2024/06/05 06:18
一.问题
当我们进行hibernate进行双向关联时,我们用json解析时进入死循环,如果我们不用json不会死循环因为不用JSON时一般有懒加载不会出现死循环。
例如下面:
Examination.java中Examination一对多questions
@OneToMany(targetEntity = Question.class, cascade = CascadeType.REMOVE, mappedBy = "examination", fetch = FetchType.LAZY) @OrderBy("id") private Set<Question> questions = new HashSet<>();
Question.java中Question多对一examination
@ManyToOne(targetEntity = Examination.class, fetch = FetchType.LAZY) @JoinColumn(name = "examination_id", referencedColumnName = "id", nullable = false) private Examination examination;
二.方法
在类的开始或者属性的前面加上@JsonIgnoreProperties注解代码如下:
Entity@Table(name = "examination", catalog = "questionnaire")@JsonIgnoreProperties(value = {"user", "hibernateLazyInitializer", "hibernateLazyInitializer", "handler", "fieldHandler"})public class Examination extends BaseBean { private static final long serialVersionUID = -2594676432596634203L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; //自测还是他测 self自测 other他测 private String type; @Column(unique = true, nullable = false) private String name; //总共的时间 private Integer allTime; public Double totalScore; @OneToMany(targetEntity = ExaminationComment.class, cascade = CascadeType.REMOVE, mappedBy = "examination", fetch = FetchType.EAGER) @OrderBy("subsection") private Set<ExaminationComment> examinationComments = new HashSet<>(); @OneToMany(targetEntity = Question.class, cascade = CascadeType.REMOVE, mappedBy = "examination", fetch = FetchType.LAZY) @OrderBy("id") private Set<Question> questions = new HashSet<>(); @ManyToOne(targetEntity = User.class, fetch = FetchType.LAZY) @JoinColumn(name = "user_id", referencedColumnName = "id", nullable = false) private User user;
@Entity@Table(name = "question", catalog = "questionnaire")@JsonIgnoreProperties(value = {"examination"})public class Question extends BaseBean { private static final long serialVersionUID = 8720345728105271415L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; //文本内容 private String title; //分数 private Double score; private String content; //正确答案 private String correctOption; private String comment; @ManyToOne(targetEntity = Examination.class, fetch = FetchType.LAZY) @JoinColumn(name = "examination_id", referencedColumnName = "id", nullable = false) private Examination examination; private String questionType; @OneToMany(targetEntity = ChoiceAnswer.class, fetch = FetchType.LAZY, mappedBy = "question", cascade = CascadeType.REMOVE) @OrderBy("theOption") private Set<ChoiceAnswer> choiceAnswers = new HashSet<>();
0 0
- springboot 使用hibernate进行双向关联json解析时死循环处理
- Hibernate双向关联 Json死循环
- Hibernate双向关联转Json死循环问题(Gson)
- hibernate双向关联和json-lib使用的死循环问题的解决
- 使用hibernate解析json时死循环问题解决
- hibernate的实体类双向关联转json报错之死循环解决方案
- 递归算法:解决hibernate实体外键关联,导致json解析死循环!
- Hibernate表关联对象转Json双向关联解析过度
- hibernate关联对象通过Springmvc转json死循环
- 使用jackson转json解决双向关联循环调用
- 关于Hibernate中自关联向前台传送json死循环的问题
- JSON解析关联类型发生死循环 There is a cycle in the hierarchy!
- hibernate取树形结构返回Json时死循环
- hibernate 双向关联(为什么不死循环)
- hibernate双向关联转化为Json数据
- Hibernate 双向关联实体转Json
- json+hibernate死循环问题的解决方法
- json+hibernate死循环问题的解决方法
- Java代理模式
- Chapter 11 Instruction Set: Addressing Modes and Formats
- Longest Substring Without Repeating Characters-LeetCode
- LintCode 170-旋转链表
- 2016这一年
- springboot 使用hibernate进行双向关联json解析时死循环处理
- spring mvc 重定向后还能传递对象数据的方法
- Unity 关于手柄摇杆和键值的测试
- Chapter 12 CPU Structure and Function
- vmware复制虚拟机出现Error:No suitable device found:no device found for connection 'System eth0'
- Chapter 13 Reduced Instruction Set Computer
- pat-a1051. Pop Sequence (25)
- Chapter 14 Instruction level parallelism and superscalar processors
- 问题描述 已知一个正整数N,问从1~N中任选出三个数,他们的最小公倍数最大可以为多少。 输入格式 输入一个正整数N。 输出格式 输出一个整数,表示你找到的最小公倍数。 样例输入 9 样例输出 5