本人使用Hibernate查询数据库得到一个集合然后想使用json lib将集合转换为json对象,来实现前台的ajax翻页
现在遇到的问题是
不使用属性过滤陷入属性级联的死循环              jsonArray = JSONArray.fromObject(usersList);net.sf.json.JSONException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.jzbr.entity.Groups.bbsusersList, no session or session was closed

使用属性过滤
                JsonConfig config = new JsonConfig();
config.setExcludes(new String[]{"bbsusersList"});
jsonArray = JSONArray.fromObject(usersList);
net.sf.json.JSONException: There is a cycle in the hierarchy!

HibernateJSON

解决方案 »

  1.   

    我看了下你的异常,你用了hibernate,异常很明确啊,org.hibernate.LazyInitializationException,lazy初始化异常,意思就是你可能用了user下级的数据,但是你在查user完毕之后就把session给关了,所以找不到,要不你就把lazy设为false,要不就在web.xml里配置延迟加载,代码如下:
    <!-- 延迟加载 -->
    <filter>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <url-pattern>*.action</url-pattern>
    </filter-mapping> <filter-mapping>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <url-pattern>*.jsp</url-pattern>
    </filter-mapping>配置了延迟加载貌似就不会出现这样的问题,大概异常的思路是这些地方,楼主!自己在具体的看看!
      

  2.   

    额 我使用的是 Hibernate  注解方式查询的数据可以属性能直接设置延迟加载?
    @Entity
    @Component("users")
    public class Users implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue
    @Column(name = "id")
    private Integer id;
    @Column(name = "username", length = 16, nullable = false, unique = true)
    private String username;
    @Column(name = "password")
    private String password;
    @Column(name = "rDate")
    private Date rDate;
    @Column(name = "state")
    private byte state;
    @ManyToOne
    @JoinColumn(name = "roles_id")
    private Roles roles;
    @OneToOne(mappedBy = "users",cascade=CascadeType.ALL)
    private BBSUsers bbsUsers;
      

  3.   

    那个注解我没有!怎么弄啊!好久没有写java了!(*^__^*) 嘻嘻
      

  4.   

    第一个就是你说的死循环,
    第二个请注意config对象未作为参数使用进去
    话说燕子还会JAVA?给我加个关注啊 ,然后才能给你发私信的。