【发布时间】:2017-01-09 00:00:29
【问题描述】:
这个特定环境下的程序: EJB3.0 + JPA + jersey Web 服务
第一个实体:
@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
如何解决我的问题?
【问题讨论】:
-
stackoverflow.com/questions/17542240/… 这可能会有所帮助。避免循环的最佳方法。
-
对于遇到此问题的任何人,最佳解决方案是:stackoverflow.com/a/18288939/6835976
标签: java json hibernate jackson