fastjson序列化hibernate查询的数据报错:failed to lazily initialize a collection of role
来源:互联网 发布:阿里云dns解析故障 编辑:程序博客网 时间:2024/05/29 02:00
原因,hibernate代理和延迟加载对象如果使用了lazy策略,那么序列化的时候要报错。
解决办法,JSON序列化的时候,如果看到返回来的属性值是hibernate代理对象或者延迟加载
对象,那么如果其值为还没有被初始化,那么不序列化。
过滤的关键类实现PropertyFilter
代码如下:
- package com.smart.oo.action.resource;
- import org.hibernate.collection.PersistentCollection;
- import org.hibernate.proxy.HibernateProxy;
- import org.hibernate.proxy.LazyInitializer;
- import com.alibaba.fastjson.serializer.PropertyFilter;
- public class SimplePropertyFilter implements PropertyFilter {
- /**
- * @param obj
- * 需要过滤的对象
- * @param s
- * 属性
- * @param obj1
- * 属性在内存中的存值 过滤不需要被序列化的属性,主要用于hibernate的代理和管理。
- * obj1是属性的值,hibernate加载出来后可能是HibernateProxy或PersistentCollection
- * 过滤它
- */
- @Override
- public boolean apply(Object obj, String s, Object obj1) {
- if (obj1 instanceof HibernateProxy) {// hibernate代理对象
- LazyInitializer initializer = ((HibernateProxy) obj1)
- .getHibernateLazyInitializer();
- if (initializer.isUninitialized()) {
- return false;
- }
- } else if (obj1 instanceof PersistentCollection) {// 实体关联一对多
- PersistentCollection collection = (PersistentCollection) obj1;
- if (!collection.wasInitialized()) {
- return false;
- }
- }
- return true;
- }
- }
使用时:
List<Resource> list = resourceService.queryAllResource(page, null);
SimplePropertyFilter filter = new SimplePropertyFilter();
String str = JSON.toJSONString(list, filter);
注:本代码本人亲测没有问题,而且在我自己的项目中使用。读者可以放心使用。
阅读全文
0 0
- fastjson序列化hibernate查询的数据报错:failed to lazily initialize a collection of role
- fastjson序列化hibernate查询的数据报错:failed to lazily initialize a collection of role
- failed to lazily initialize a collection of role:报错
- SSH报错之 hibernate.LazyInitializationException: failed to lazily initialize a collection of role: XXXX
- hibernate报错failed to lazily initialize a collection of role
- Hibernate问题 failed to lazily initialize a collection of role
- Hibernate多对多、一对多查询出现failed to lazily initialize a collection of role
- Hibernate 延迟加载的错误 failed to lazily initialize a collection of role [Hibernate]
- OneToMany字段序列化 failed to lazily initialize a collection of role
- hibernate一对多取到的set集合 failed to lazily initialize a collection of role
- hibernate 延迟加载的错误 failed to lazily initialize a collection of role
- hibernate 延迟加载的错误 failed to lazily initialize a collection of role
- hibernate 延迟加载的错误 failed to lazily initialize a collection of role
- hibernate懒加载的问题,failed to lazily initialize a collection of role
- SSH整合报错:failed to lazily initialize a collection of role
- failed to lazily initialize a collection of role:
- failed to lazily initialize a collection of role:
- failed to lazily initialize a collection of role
- Rhyme/Linux /etc/fstab 配置文件解释
- WeakReference
- preg_replace()替换成preg_replace_callback() php5.5之后
- fastjson在使用hibernate关系模型转json出现$ref解决办法
- ResourceBundle missingresourceexception
- fastjson序列化hibernate查询的数据报错:failed to lazily initialize a collection of role
- 10 个快速提升技术水平的方法
- Eclipse配置多域名启动项目详细步骤
- C读书笔记
- [shell]curl结果获取http header的问题
- 171024-对do-while,while,for语句的复习
- RDD日常操作函数详解
- insmod 的一些权限
- 欢迎使用CSDN-markdown编辑器