Spring-data-jpa多對多雙向關聯時出現死循環,內存溢出

原代碼:java

SysUser類:json

@Entity
public class SysUser implements Serializable{

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue
	private Long uid;// 用戶id
	
	@Column(unique=true)
	private String username;// 帳戶
	
	private String name;// 名稱
	
	private String password;
	
	private String salt;
	
	private byte state;
	
	@ManyToMany(fetch=FetchType.EAGER)
	@JoinTable(name="SysUserRole", 
				joinColumns= {@JoinColumn(name="uid", referencedColumnName="uid")}, 
				inverseJoinColumns= {@JoinColumn(name="roleId", referencedColumnName="id")})
	private List<SysRole> roleList;// 一個用戶具備多個角色

SysRole類:fetch

@Entity
public class SysRole implements Serializable{

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue
	private Long id; 
	
	private String role;
	private String description;
	private Boolean available = Boolean.FALSE;// 是否可用
	
	// 多對多
	@ManyToMany(fetch=FetchType.EAGER)
	@JoinTable(name="SysRolePermission", 
			joinColumns= {@JoinColumn(name="roleId", referencedColumnName="id")}, 
			inverseJoinColumns= {@JoinColumn(name="permissionId", referencedColumnName="id")})
	private List<SysPermission> permissions;
	
	// 多對多
	@ManyToMany
	@JoinTable(name="SysUserRole", 
				joinColumns= {@JoinColumn(name="roleId", referencedColumnName="id")}, 
				inverseJoinColumns= {@JoinColumn(name="uid", referencedColumnName="uid")})
	private List<SysUser> users;

此處省略set get 和toString方法。ui

查詢一個user時返回數據以下:url

{"uid":1,"username":"admin","name":"admin","password":"admin","salt":"d3c59d25033dbf980d29554025c23a75","state":1,"roleList":[{"id":1,"role":"admin","description":"管理員","available":true,"permissions":[{"id":1,"name":"用戶管理","resourceType":"menu","url":"userInfo/userList","permission":"userInfo:view","parentId":0,"parentIds":"0/","available":true}]},{"id":2,"role":"vip","description":"VIP會員","available":true,"permissions":[{"id":1,"name":"用戶管理","resourceType":"menu","url":"userInfo/userList","permission":"userInfo:view","parentId":0,"parentIds":"0/","available":true}]}]}{"uid":1,"username":"admin","name":"admin","password":"admin","salt":"d3c59d25033dbf980d29554025c23a75","state":1,"roleList":[{"id":1,"role":"admin","description":"管理員","available":true,"permissions":[{"id":1,"name":"用戶管理","resourceType":"menu","url":"userInfo/userList","permission":"userInfo:view","parentId":0,"parentIds":"0/","available":true}]},{"id":2,"role":"vip","description":"VIP會員","available":true,"permissions":[{"id":1,"name":"用戶管理","resourceType":"menu","url":"userInfo/userList","permission":"userInfo:view","parentId":0,"parentIds":"0/","available":true}]}]}{"uid":1,"username":"admin","name":"admin","password":"admin","salt":"d3c59d25033dbf980d29554025c23a75","state":1,"roleList":[{"id":1,"role":"admin","description":"管理員","available":true,"permissions":[{"id":1,"name":"用戶管理","resourceType":"menu","url":"userInfo/userList","permission":"userInfo:view","parentId":0,"parentIds":"0/","available":true}]},{"id":2,"role":"vip","description":"VIP會員","available":true,"permissions":[{"id":1,"name":"用戶管理","resourceType":"menu","url":"userInfo/userList","permission":"userInfo:view","parentId":0,"parentIds":"0/","available":true}]}]}...

後臺報錯StackOverFlow3d

問題緣由:根據查詢結果可知,查詢一定是經歷了死循環,緣由是user類中有一個role集合,role類中有一個user集合code

解決方法:中斷循環便可,首先這不是jpa的問題,是序列化的問題,咱們只要中斷序列化就能夠了,在user類中使用jackson的註解ip

@JsonIgnoreProperties(value = {"users"})get

代碼以下:it

@JsonIgnoreProperties(value = {"users"})
	@ManyToMany(fetch=FetchType.EAGER)
	@JoinTable(name="SysUserRole", 
				joinColumns= {@JoinColumn(name="uid", referencedColumnName="uid")}, 
				inverseJoinColumns= {@JoinColumn(name="roleId", referencedColumnName="id")})
	private List<SysRole> roleList;
相關文章
相關標籤/搜索