JSON 在做fromObject的时候抛出一个异常

我在网上看了一些资料,似乎说跟Hibernate的一些机制有关所导致,但看了一些解决方案都没多大帮助。
[b]异常信息:[/b]
net.sf.json.JSONException: There is a cycle in the hierarchy!
[b]问题概述:[/b]
我希望前台点击某个BUTTON后通过AJAX调用方法查询出所有的教师信息。
List已经得到,但将其进行JSON的转换时有异常抛出。
异常信息为net.sf.json.JSONException: There is a cycle in the hierarchy!
后台持久层采用了Hibernate,实体Bean由MyEclipse根据DB反向生成。
[b]异常代码段:[/b]
就是在 JSONArray jslist = JSONArray.fromObject(list); 这一句出问题
[code="java"]
public class Stu_GetAllTeachers_AJAX extends Action {
private TeachersServiceImpl tsi;
public TeachersServiceImpl getTsi() {[code="java"][/code]
return tsi;
}
public void setTsi(TeachersServiceImpl tsi) {
this.tsi = tsi;
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception{
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
List list=new ArrayList();
list=tsi.getAllTeachers();
JSONArray jslist = JSONArray.fromObject(list);
response.setContentType("application/javascript;charset=utf-8");
PrintWriter out = response.getWriter();
out.print(jslist);
return null;
}
}
[/code]
[b]相关代码段:[/b]
Teachers实体BEAN的部分代码
[code="java"]
public class Teachers implements java.io.Serializable {

// Fields

private String id;
private Positions positions;
private String name;
private String sex;
private Date birth;
private String edulevel;
private String discovery;
private String phone;
private String course;
private String pwd;
private Integer flag;
private Set thesises = new HashSet(0);
private Set results = new HashSet(0);

// Constructors

[/code]

把Teachers实体的Hibernate配置文件发一下,不知道有没有用。
[code="java"]

















































[/code]
[b]问题补充:[/b]
嗯,我发现了,是嵌套了,我的Thesis和Result两个实体BEAN里都定义了Teachers。
可是,我具体要怎么改呢?说具体点好吗?
Thesis&Result
[code="java"]
public class Result implements java.io.Serializable {

// Fields

private String studentId;
private Thesis thesis;
private Teachers teachers;
private Students students;
private Byte change;

[/code]

[code="java"]
public class Thesis implements java.io.Serializable {

// Fields

private String id;
private Major major;
private Teachers teachers;
private String title;
private String discovery;
private String doc;
private Integer stuAllownum;
private String description;
private Set results = new HashSet(0);

[/code]

既然在Thesis&Result中定义了对Teacher的引用,那就没有必要在Teachers中再定义对Thesis和Result的集合了。直接去掉[quote]
private Set thesises = new HashSet(0);

private Set results = new HashSet(0);[/quote]定义即可。
如需要用到这两个类的集合实例时,到各自DAO定义中,利用GetByTeacher(Teacher teaher)方法来得到。
不过,建议给这两个类传递teacher表的主键,而非类实例。

有循环套用就会抛出这个异常..json在fromObject时会遍历整个Teachers对象..如果Teachers有循环引用就不行了,如Teachers下面的属性里还包含自己的Teachers就不行了..这样会无限循环的遍历下去了

[quote] private Set thesises = new HashSet(0);

private Set results = new HashSet(0); [/quote]
应该是这个地方的原因,JSON系列化的时候会遍历你的list,因为list是一个Collection集合类型,JSONArray会调用_fromCollection方法,这个方法定义:
[code="java"] private static JSONArray _fromCollection( Collection collection, JsonConfig jsonConfig ) {
fireArrayStartEvent( jsonConfig );

  if( !addInstance( collection ) ){
     try{
        return jsonConfig.getCycleDetectionStrategy()
              .handleRepeatedReferenceAsArray( collection );
     }catch( JSONException jsone ){
         ...
     }...
  }...

}[/code]
handleRepeatedReferenceAsArray在util包CycleDetectionStrategy中定义,定义了对集合类型的处理方法,其中有一句:
The JSON spec forbides cycles in a hierarchy and most parsers will raise and error when a cycle is detected.
也就是说,在集合类中,每个元素不能再次包含Collection类实例。
[b]解决办法:[/b]
edu.bhu.Kupid.domain.Thesis中定义对Teacher的引用。这样在编程中会更加灵活,同时也实现了主键约束。