在这个特定环境下的程序: EJB3.0 + JPA + Jersey Web Service
第一个实体:
@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)
如果我将fetch 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
如何解决我的问题?