Kupid
卡彼得
2009-04-12 11:48

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

  • hibernate

我在网上看了一些资料,似乎说跟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]

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答

为你推荐

换一换