hibernate对象懒加载,json序列化失败

 //net.sf.json 解决办法


 JsonConfig config = new JsonConfig(); 
 config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT); 
 config.setExcludes(new String[]{"handler","hibernateLazyInitializer"}); 
 //原文:https://zhidao.baidu.com/question/504539752.html
 ```


//fastjson解决办法

```package com.pbh.aop;

import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.LazyInitializer;

import com.alibaba.fastjson.serializer.PropertyFilter;

public class SimplePropertyFilter implements PropertyFilter {

    @Override
    public boolean apply(Object object, String name, Object value) {
        if (value instanceof HibernateProxy) {//hibernate代理对象  
            LazyInitializer initializer = ((HibernateProxy) value).getHibernateLazyInitializer();  
            if (initializer.isUninitialized()) {  
                return false;  
            }  
        } else if (value instanceof PersistentCollection) {//实体关联集合一对多等  
            PersistentCollection collection = (PersistentCollection) value;  
            if (!collection.wasInitialized()) {  
                return false;  
            }  
            Object val = collection.getValue();  
            if (val == null) {  
                return false;  
            }  
        }  
        return true;
    }
    }




<div class="se-preview-section-delimiter">div>
        String json = JSON.toJSONString(tags, filter);

http://blog.csdn.net/xiaozaq/article/details/53560000

```     
        SimplePropertyFilter filter = new SimplePropertyFilter();  
        String json = JSON.toJSONString(tags, filter);

原文:http://blog.csdn.net/xiaozaq/article/details/53560000

你可能感兴趣的:(hibernate)