json解决hibernate中级联对象延迟加载问题net.sf.json.JSONException: org.hibernate.LazyInit
来源:互联网 发布:图像卷积算法介绍 编辑:程序博客网 时间:2024/05/22 17:05
hibernate使用CGLIB把POJO的domain对象动态代理,实现它的魔法,但是给JSON的序列化带来了麻烦,因为JSON无法对 lazy的属性进行序列化。有以下的四个方法可以解决hibernate的序列化问题
domain类实现JSONString接口
- 建立JsonConfig实例,并配置属性排除列表
用属性过滤器
- 写一个自定义的JsonBeanProcessor
1. 实现JSONString接口是侵入性最强的方法
public class Person implements JSONString {private String name;private String lastname;private Address address;// getters & setterspublic String toJSONString() {return "{name:'"+name+"',lastname:'"+lastname+"'}";}}
2.第二种方法通过jsonconfig实例,对包含和需要排除的属性进行方便添加删除
public class Person {private String name;private String lastname;private Address address;// getters & setters}JsonConfig jsonConfig = new JsonConfig();jsonConfig.setExclusions( new String[]{ "address" } );Person bean = ;JSON json = JSONSerializer.toJSON( bean, jsonConfig );
注意:这种方法不区分目标类,就是说如果有2个bean当中都存在“address”属性,那么采用这种方法,这两个bean中的address属 性都将被排除
3. 使用propertyFilter可以允许同时对需要排除的属性和类进行控制,这种控制还可以是双向的,也可以应用到json字符串到java对象
public class Person {private String name;private String lastname;private Address address;// getters & setters}JsonConfig jsonConfig = new JsonConfig();jsonConfig.setJsonPropertyFilter( new PropertyFilter(){public boolean apply( Object source, String name, Object value ){// return true to skip namereturn source instanceof Person && name.equals("address");}});Person bean = ;JSON json = JSONSerializer.toJSON( bean, jsonConfig )
4. 最后来看JsonBeanProcessor,这种方式和实现JsonString很类似,返回一个代表原来的domain类的合法JSONOBJECT
public class Person {private String name;private String lastname;private Address address;// getters & setters}JsonConfig jsonConfig = new JsonConfig();jsonConfig.registerJsonBeanProcesso<wbr>r( Person.class,new JsonBeanProcessor(){public JSONObject processBean( Object bean, JsonConfig jsonConfig ){if( !(bean instanceof Person) ){return new JSONObject(true);}Person person = (Person) bean;return new JSONObject().element( "name", person.getName() ).element( "lastname", person.getLastname() );}});Person bean = ;JSON json = JSONSerializer.toJSON( bean, jsonConfig );</wbr>
0 0
- json解决hibernate中级联对象延迟加载问题net.sf.json.JSONException: org.hibernate.LazyInit
- json解决hibernate中级联对象延迟加载问题net.sf.json.JSONException: org....
- json解决hibernate中级联对象延迟加载问题net.sf.json.JSONException: org....
- json解决hibernate中级联对象延迟加载有关问题
- Hibernate 延迟加载导致的问题 net.sf.json.JSONException: There is a cycle in the hierarchy!
- net.sf.json.JSONException
- net.sf.json.JSONException: There is a cycle in the hierarchy!---数据间存在级联关系,而互相嵌套引用,在hibernate中极容易嵌套而抛
- json中net.sf.json.JSONException: There is a cycle in the hierarchy!的解决
- JSON中 net.sf.json.JSONException: java.lang.NoSuchMethodException异常
- JSON处理Hibernate实体类net.sf.json.JSONException: There is a cycle in the hierarchy异常
- 异常 org.apache.struts2.json.JSONException: org.hibernate.LazyInitializationException:
- 异常 org.apache.struts2.json.JSONException: org.hibernate.LazyInitializationException
- json与Java对象转化出现net.sf.json.JSONException: There is a cycle in the hierarchy!问题
- net.sf.json.JSONException: java.lang.NoSuchMethodException
- net.sf.json.JSONException: Object is null
- net.sf.json.JSONException: Object is null
- net.sf.json.JSONException: Unquotted string "E44C2B0168A550F6503F59CCD56FC49B"
- net.sf.json.JSONException: java.lang.NoSuchMethodException
- git bisect
- Unity接入Admob广告教程IOS篇
- 5.2类与对象---对象和C语言中的指针变量的比较
- iOS 【UIKit-一个控件用肉眼看不见,有哪些可能】
- 10 BEST WEBSITES FOR ENGLISH TEACHERS 10 Best Websites for English Teachers
- json解决hibernate中级联对象延迟加载问题net.sf.json.JSONException: org.hibernate.LazyInit
- Immutable 详解及 React 中实践
- hrbust 2024/hdu2899 Strange fuction【三分】
- HTMl5的sessionStorage和localStorage
- 探秘Java中String、StringBuilder以及StringBuffer
- (4) Python 内置类型 –- 字典
- 构建高并发高可用的电商平台架构实践
- 初步了解ReactJS
- osgi实例讲解