@JsonIgnore @JsonIdentityInfo 处理Hibernate 循环引用的问题
来源:互联网 发布:阿里云后端框架 编辑:程序博客网 时间:2024/05/17 20:23
enterprise和user一对一的关系:
@Entity@Table(name = "enterprise")public class Enterprise extends BaseEntity { private static final long serialVersionUID = 1L; private User user; private String name; @OneToOne(mappedBy = "enterprise") public User getUser() { return user; } public void setUser(User user) { this.user = user; } ...setter/getter}
@Entity@Table(name = "APP_USER")public class User extends BaseEntity { @NotEmpty @Column(name = "FIRST_NAME", nullable = false) private String firstName; @Column(name = "LAST_NAME", nullable = true) private String lastName; @OneToOne private Enterprise enterprise;}
在controller中,使用ResponseEntity来返回enterprise转换为json之后的字符串。如果不做任何处理,因为enterprise拥有user字段,而user也拥有enterprise字段,自此出现了循环引用的问题,程序将会出错。
使用@JsonIgnore
@JsonIgnore@OneToOne(mappedBy = "enterprise")public User getUser() { return user;}
在getUser方法上添加@JsonIgnore注释,返回的enterprise json对象中,将不包括user对象。
但是如果我们希望在enterprise json对象中包括user的json对象该怎么办呢?
使用@JsonIdentityInfo
Annotation used for indicating that values of annotated type or property should be serializing so that instances either contain additional object identifier (in addition actual object properties), or as a reference that consists of an object id that refers to a full serialization. In practice this is done by serializing the first instance as full object and object identity, and other references to the object as reference values.
在user类或者enterprise类上增加该注解即可。
@Entity@Table(name = "enterprise")@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")public class Enterprise extends BaseEntity
- @JsonIgnore @JsonIdentityInfo 处理Hibernate 循环引用的问题
- 循环引用的处理
- 循环引用的问题...
- 解决hibernate @OneToOne 无限循环引用问题
- spring循环引用的问题
- block 循环引用的问题
- Block的循环引用问题
- Block的循环引用问题
- block的循环引用问题
- Block的循环引用问题
- Block的引用循环问题
- block循环引用的问题
- NSTimer循环引用的问题
- JS循环引用的问题
- NSTimer的循环引用问题
- 引用计数法的循环引用问题
- Objective-C 循环引用的处理
- Spring中bean循环引用的处理
- 语义化版本编号
- 最短路径
- Android Touch事件分发笔记
- C# 多文件与二进制互转数据库写入读出(上传文件二进制方式到DB中,下载DB中的二进制文件到本地)
- java基础教程:循环结构(10)
- @JsonIgnore @JsonIdentityInfo 处理Hibernate 循环引用的问题
- CMake + MinGW + OpenCV + CodeBlocks Windows下环境搭建
- 交叉编译出错
- Java基础回顾——重难点易错点【2】
- android shape 使用详解
- js中this的使用案例整理
- MFC学习笔记(一):模态对话框与非模态对话框的区别、创建及显示
- idea 从远程仓库导入git项目
- git学习实践