這個特定的環境下的程序: EJB3.0 + JPA +球衣Web服務JPA雙向一對多JSON無限循環
第一實體:
@Entity
@Table(name = "student_by_test_yao")
public class StudentTest implements Serializable {
@Id
@GeneratedValue
private Integer id;
private String name;
@ManyToOne
@JoinColumn(name = "class_id")
private ClassTest classes;
public StudentTest() {}
}
第二實體:
@Entity
@Table(name = "class_by_test_yao")
public class ClassTest implements Serializable{
@Id
@GeneratedValue
private Integer id;
private String name;
@OneToMany(mappedBy = "classes",cascade = CascadeType.ALL, fetch=FetchType.EAGER)
private List<StudentTest> students;
public ClassTest() {}
}
當我拿到ClassTest的學生名單時。 的例外是:
com.fasterxml.jackson.databind.JsonMappingException:
Infinite recursion (StackOverflowError)
如果我改變了獲取FetchType.LAZY唯一的例外是:
org.hibernate.LazyInitializationException:
failed to lazily initialize a collection of role:
cn.gomro.mid.core.biz.goods.test.ClassTest.students,
could not initialize proxy - no Session
如何解決我的問題呢?
@JsonIgnore是沒用的。仍然在Excepton的'org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:167)引起:com.fasterxml.jackson.databind.JsonMappingException: 無限遞歸(StackOverflowError)' –
嗯,奇怪。它幫助我,當我有同樣的異常 –