Json   发布时间:2022-04-22  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了fastjson序列化hibernate持久化对象时忽略代理的懒加载对象大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

过滤懒加载代理对象(该端代码原文地址找不到了

package com.pbh.filter;

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

import com.alibaba.fastjson.serializer.PropertyFilter;

/** * * Description: 过滤Hibernate懒加载不能序列化对象 * @author PanBaihui * @time 2017年9月15日 下午4:15:45 */
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;
    }

}

通用实现

public class ComplexPropertyPreFilter implements PropertyFilter {  
    private Map<Class<?>,Set<String>> includeMap = new HashMap<Class<?>,Set<String>>();  
    //@Override 
    public Boolean apply(Object source,Object value) {  
        for(Entry<Class<?>,Set<String>> entry : includeMap.entrySet()) {  
            Class<?> class1 = entry.getKey();  
            if(source.getClass() == class1){  
                Set<String> fields = entry.getValue();  
                for(String field : fields) {  
                    if(field.equals(Name)){  
                        return false;  
                    }  
                }  
            }  
        }  
        return true;  
    }  

    public ComplexPropertyPreFilter(Map<Class<?>,Set<String>> includeMap){  
        this.includeMap = includeMap;  
    }  
}

原文:http://code.js-code.com/article/p-skspthzp-bgo.html

大佬总结

以上是大佬教程为你收集整理的fastjson序列化hibernate持久化对象时忽略代理的懒加载对象全部内容,希望文章能够帮你解决fastjson序列化hibernate持久化对象时忽略代理的懒加载对象所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。