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

原文地址:https://blog.csdn.net/jiangshanwe/article/details/72994159

最新文章

  1. 清除webBrowser 缓存和Cookie的解决方案
  2. html5-websocket初探
  3. 面向服务的体系结构(SOA)——(5)关于MEP(Message Exchange Patterns)
  4. SQL Server 一列或多列重复数据的查询,删除
  5. javaScript call 函数的用法说明
  6. jquery serialize()方法的扩展
  7. 3、VS2010+ASP.NET MVC4+EF4+JqueryEasyUI+Oracle项目开发之——用户登录
  8. linux ifconfig命令使用详解
  9. js:进一步关闭(范围:下一个)
  10. Memory Limits for Windows and Windows Server Releases
  11. [转载] sublime text 2 调试python时结果空白
  12. 项目总结2——mybatis配置的理解
  13. Java:全局变量(成员变量)与局部变量
  14. windows下php7.1安装redis扩展以及redis测试使用全过程
  15. 【 剑指Offer 1 】数据结构
  16. Android 工程目录
  17. MySQL中数据的基本查询方式
  18. tensorflow的一些基础用法
  19. mysql exists 如何使用
  20. python之模块calendar(汇集了日历相关的操作)

热门文章

  1. Leetcode883.Projection Area of 3D Shapes三维形体投影面积
  2. SQL SERVER 2008 R2 插入数据非常慢
  3. spring-cloud-zuul跨域问题解决
  4. database homework3
  5. 2019.10.22 用TCP实现服务端并发接收
  6. 初探postman
  7. Activity基本类分析
  8. python系列之(4)豆瓣图书《平凡的世界》书评及情感分析
  9. 主攻互动娱乐和视频自媒体,新浪SHOW是不是桩好生意?
  10. 笔记: CC2540 和 CC2541 的区别