解决json转换hibernate 结果集对象
public static JsonConfig getCommonConfig() {/*---- 做属性配置 start------*/JsonConfig conf = new JsonConfig();conf.registerJsonBeanProcessor( org.hibernate.proxy.HibernateProxy.class, new HibernateJsonBeanProcessor()); conf.setJsonBeanProcessorMatcher(new HibernateJsonBeanProcessorMatcher());return conf;}}class HibernateJsonBeanProcessorMatcher extends JsonBeanProcessorMatcher { @Override public Object getMatch(Class target, Set set) { if (target.getName().contains("$$EnhancerByCGLIB$$")) { return org.hibernate.proxy.HibernateProxy.class; } return DEFAULT.getMatch(target, set); } }class HibernateJsonBeanProcessor implements JsonBeanProcessor { public JSONObject processBean(Object obj, JsonConfig jsonConfig) { LazyInitializer lazyInitializer = ((HibernateProxy)obj).getHibernateLazyInitializer(); if(lazyInitializer.isUninitialized()) { } return new JSONObject(); } }/** * * 功能描述: 解决hibernate 延迟加载对象问题 * @param dataList 数据集 * @param clazz 结果集内对象的class * @return: void * @author: lb * @version: 2.0 */public static List listCrawl(List dataList,Class clazz){String ss="java.lang.String,java.util.Date,java.lang.Integer,java.lang.Long,int,float,long";String filterType="java.util.Map,java.util.HashMap,java.util.Set,java.util.HashSet";try{for(int i=0;i<dataList.size();i++){Object originalObj=dataList.get(i);//原始数据对象Method[] originalMethods= clazz.getDeclaredMethods();//原始数据对象的方法for(Method originalMethod:originalMethods){System.out.println("original MethodName: "+originalMethod.getName());if(originalMethod.getName().contains("get")){System.out.println("getMethod returnType: "+originalMethod.getReturnType().getName());String returnType=originalMethod.getReturnType().getName();//get方法返回的对象类型if(!ss.contains(returnType)&&!filterType.contains(returnType)){//不是基本类型和Set Map等集合,即是二级对象Object proxyObjct=originalMethod.invoke(originalObj, null);//二级hibernate代理对象if(proxyObjct!=null){//二级代理对象是否为空Class proxyClass=proxyObjct.getClass();Method[] proxyMethods=proxyClass.getDeclaredMethods();Class tempClazz=Class.forName(returnType);//创建一个hibernate代理的原始二级对象Object tempObject=tempClazz.newInstance();//创建一个hibernate代理的原始二级对象for(Method proxyMethod:proxyMethods ){//if(ss.contains(proxyMethod.getReturnType().getName())){//只抓取hibernate代理的二级对象的基础数据if(proxyMethod.getName().contains("get")){Object returnValue=proxyMethod.invoke(proxyObjct, null);//System.out.println("proxy returnValue:"+returnValue);String setMethod=proxyMethod.getName().replace("get", "set");System.out.println("setMethod name:"+setMethod);Method tempMethod=tempClazz.getDeclaredMethod(setMethod, proxyMethod.getReturnType());tempMethod.invoke(tempObject, returnValue);}}}Method originalMethod1=clazz.getDeclaredMethod(originalMethod.getName().replace("get", "set"), originalMethod.getReturnType());originalMethod1.invoke(originalObj, tempObject);//为原始对象重新装入一个非hibernate代理对象}}}}}}catch(Exception e){System.out.println("处理加载对象出现问题");e.printStackTrace();}return dataList;} 1 楼 01jiangwei01 2011-09-12 怎么没写使用方式啊?? 2 楼 cqu903 2012-02-16 反射的性能你测试下就知道了,我之前试过,差了1000倍都不止