java 解决实体类序列化时,报failed to lazily initialize 的问题
具体报错信息:
org.codehaus.jackson.map.JsonMappingException: failed to lazily initialize a collection of role: com.girltest.entity.Test2Boy.conventions, could not initialize proxy - no Session (through reference chain: java.util.HashMap["recordList"]->java.util.ArrayList[0]->com.girltest.entity.Test2Boy["conventions"])
原因:因为实体类中有hibernate懒加载的成员变量
解决方法:
json序列化之前 ,把懒加载的成员变量设置为null
/*** * get all field ,including fields in father/super class * * @param clazz * @return */ public static List<Field> getAllFieldList(Class<?> clazz) { if (clazz == null) { return null; } List<Field> fieldsList = new ArrayList<Field>();// return object Class<?> superClass = clazz.getSuperclass();// father class if(ValueWidget.isNullOrEmpty(superClass)){ return null; } if (!superClass.getName().equals(Object.class.getName()))/* * java.lang.Object */{ // System.out.println("has father"); fieldsList.addAll(getAllFieldList(superClass));// Recursive } Field[] fields = clazz.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; // 排除因实现Serializable 接口而产生的属性serialVersionUID if (!field.getName().equals("serialVersionUID")) { fieldsList.add(field); } } return fieldsList; } /*** * 过滤hibernate懒加载的成员变量<br /> * org.codehaus.jackson.map.JsonMappingException: failed to lazily initialize a collection of role: com.girltest.entity.Test2Boy.conventions, could not initialize proxy - no Session (through reference chain: java.util.HashMap["recordList"]->java.util.ArrayList[0]->com.girltest.entity.Test2Boy["conventions"]) * * @param obj * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalArgumentException * @throws IllegalAccessException */ public static void skipHibernatePersistentBag(Object obj) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { if (obj == null) { return; } List<Field> fieldsList = getAllFieldList(obj.getClass()); if (ValueWidget.isNullOrEmpty(fieldsList)) { return; } for (int i = 0; i < fieldsList.size(); i++) { Field f = fieldsList.get(i); Object vObj = getObjectValue(obj, f); if (null != vObj && vObj.getClass().getName().equals("org.hibernate.collection.internal.PersistentBag")) { f.setAccessible(true); f.set(obj, null); } } } try { ReflectHWUtils.skipHibernatePersistentBag(object); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); }
相关推荐
npm install --save react-lazily-render 用法 () import React from 'react' ; import LazilyRender from 'react-lazily-render' ; ...lots of content... < LazilyRender xss=removed> } content = { ...
committed, the command line prompt script failed to notice the current status, which has been improved. * Many GIT_TEST_* environment variables control various aspects of how our tests are run, ...
JavaScript不支持支配量词,所以在JavaScript中无法直接演示其行为,但在Java中可以使用`Pattern`和`Matcher`类来实现。 在实际应用中,选择贪婪匹配还是惰性匹配取决于需求。贪婪匹配通常用于获取尽可能长的匹配,...
懒惰地ReactIMG React Lazily IMG是一个React Wrapper组件,用于延迟加载图像。 目标是使用方便且已知的标准HTML标记,并只是延迟加载它们。特征图片标签和IMG srcset支持Webp检测占位符HTML && CSS图像支持下载图像...
Compute values lazily and avoid unnecessary memory allocations using generators Write concurrent code that doesn't feel like concurrent code by abstracting away boilerplate chores Leverage true ...
The aim is to provide, lazily evaluated, pull-based datastream support with the same naming as in RxJava mainly for the pre-Java-8 world. The Stream API in Java 8 is not exactly the same thing because...
Slicing rows lazily Getting ready How to do it... How it works... There's more... Slicing lexicographically Getting ready How to do it... How it works... There's more... 5. Boolean Indexing ...
Allow java.time types for setting the Last-Modified header [SPR-17571] #22103 StringHttpMessageConverter should assume charset UTF-8 for application/json [SPR-17568] #22100 NettyDataBufferFactory.join...
- **.NET as a Reaction to the Java World**: Microsoft’s response to Sun Microsystems’ Java platform, aiming to provide a robust framework for building and running applications. - **The Open Source...
-- data-qazy is set to true means to load it lazily. Set it to false if you don't want to load it lazily. --> <!-- A default placeholder is used. To change the placeholder, assign the variable ...
为了解决这个问题,我们可以在Spring Boot的配置中调整Tomcat的相关设置。在`FileUploadWebApplication`类中添加如下@Bean: ```java @Bean public TomcatServletWebServerFactory tomcatEmbedded() { ...
- 完成句子练习可以帮助巩固词汇的应用,如:"The young man rose lazily and was not willing to greet us."(年轻人懒洋洋地起身,并不愿意向我们打招呼。),"This appointment was greeted with relief."(这个...
// Subscribes to a Pub/Sub channel using the internal, lazily initialized SubscriberClient redis.subscriber.subscribe("My Channel") { case message @ PubSubMessage.Message(channel, ...
- **JPA**:支持Java Persistence API,提供了一种统一的Java对象持久化机制。 - **Asynchronous JMS**:增强了对JMS消息服务的支持,包括异步消息处理能力。 - **JDBC**:改进了JDBC操作,提升了性能和易用性。 ##...
lazily. Copyright (c) 2009 Armando Blancas. All rights reserved. The use and distribution terms for this software are covered by the Eclipse Public License 1.0 ...
Lazily instantiate your dependencies when needed Stop caring about dependency initialization order Easily bind classes or interfaces to their instance or provider Easily debug your dependency bindings...
10. 系动词与形容词:"said lazily",系动词said后接形容词lazily,表示懒洋洋地说。 11. 听到某人的话:"When hearing these words",现在分词短语作时间状语。 12. 介词短语:"to his surprise",介词短语表示令他...
- **Lazily-instantiated beans**: 延迟加载Bean,直到第一次请求时才初始化。 - **Autowiring collaborators**: 自动装配协作对象,简化了配置。 - **Checking for dependencies**: 检查Bean是否存在未满足的依赖。...
名称 Cinnamon-简约的部署工具 概要 use strict; use warnings; # Exports some commands use Cinnamon::DSL; my $application = 'My::App';...# Lazily evaluated if passed as a code set lazy_value => sub { #.
在实例化类模板时,编译器会根据模板实例化的具体类型来生成相应的类,这个过程被称为模板实例化。例如,当`Operator`被用作`int`类型时,编译器会生成一个专门处理`int`的类,而当它被用于`std::string`类型时,又...