循环引用:实体A与实体B有关系,A中有B作为字段,B中有A作为一个字段。查询A对象后,将A对象转化为JSON格式数据时,会因为序列化过程中导致A中有B字段,B字段中又有A,这样就引起了循环引用的问题!!
即如下的解释:
如果A对象持有B的引用,B对象持有A的引用,这样就形成了循环引用,如果直接使用json-lib转换,会报错:
net.sf.json.JSONException: There is a cycle in the hierarchy!
错误由来:
maven搭建的项目 使用spring+hibernate+springMVC
两个实体类: Product产品类:Disease疾病类 (1:n)关系
Product.java实体类:
1 package com.agen.entity; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 import javax.persistence.CascadeType; 7 import javax.persistence.Column; 8 import javax.persistence.Entity; 9 import javax.persistence.FetchType; 10 import javax.persistence.GeneratedValue; 11 import javax.persistence.Id; 12 import javax.persistence.OneToMany; 13 import javax.persistence.Table; 14 15 import org.hibernate.annotations.GenericGenerator; 16 17 import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 18 19 /** 20 * Product entity. @author MyEclipse Persistence Tools 21 */ 22 @Entity 23 @Table(name = "product", catalog = "biologyinfo") 24 public class Product implements java.io.Serializable { 25 26 // Fields 27 28 /** 29 * 30 */ 31 private static final long serialVersionUID = 1L; 32 private String productId; 33 private String productName; 34 private String productPath; 35 private Integer productOrder; 36 private String productCre; 37 private Set<Disease> diseases = new HashSet<Disease>(0); 38 39 // Constructors 40 41 /** default constructor */ 42 public Product() { 43 } 44 45 /** full constructor */ 46 public Product(String productName, String productPath, 47 Integer productOrder, String productCre, Set<Disease> diseases) { 48 this.productName = productName; 49 this.productPath = productPath; 50 this.productOrder = productOrder; 51 this.productCre = productCre; 52 this.diseases = diseases; 53 } 54 55 // Property accessors 56 @GenericGenerator(name = "generator", strategy = "uuid.hex") 57 @Id 58 @GeneratedValue(generator = "generator") 59 @Column(name = "productId", unique = true, nullable = false, length = 36) 60 public String getProductId() { 61 return this.productId; 62 } 63 64 public void setProductId(String productId) { 65 this.productId = productId; 66 } 67 68 @Column(name = "productName", length = 30) 69 public String getProductName() { 70 return this.productName; 71 } 72 73 public void setProductName(String productName) { 74 this.productName = productName; 75 } 76 77 @Column(name = "productPath", length = 200) 78 public String getProductPath() { 79 return this.productPath; 80 } 81 82 public void setProductPath(String productPath) { 83 this.productPath = productPath; 84 } 85 86 @Column(name = "productOrder") 87 public Integer getProductOrder() { 88 return this.productOrder; 89 } 90 91 public void setProductOrder(Integer productOrder) { 92 this.productOrder = productOrder; 93 } 94 95 @Column(name = "productCre", length = 500) 96 public String getProductCre() { 97 return this.productCre; 98 } 99 100 public void setProductCre(String productCre) { 101 this.productCre = productCre; 102 } 103 104 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "product") 105 public Set<Disease> getDiseases() { 106 return this.diseases; 107 } 108 109 public void setDiseases(Set<Disease> diseases) { 110 this.diseases = diseases; 111 } 112 113 }