Original http://blog.csdn.net/skmbw/article/details/12650827
Fastjson serialization Hibernate proxy and deferred load object appears org.hibernate.LazyInitializationException:failed to lazily initialize a collection of Role:com.eecn.warehouse.api.model.Tags.childTags, could not initialize proxy-no session.
For this extension point that can be given using Fastjson, implement the PropertyFilter interface and filter the attributes that you do not want to serialize.
the following implementation, if it is a hibernate proxy object or a deferred load object, is filtered out and not serialized. If there is a value, it is serialized.
Package Com.test.json;
Import org.hibernate.collection.spi.PersistentCollection;
Import Org.hibernate.proxy.HibernateProxy;
Import Org.hibernate.proxy.LazyInitializer;
Import Com.alibaba.fastjson.serializer.PropertyFilter;
The public class Simplepropertyfilter implements PropertyFilter {/** * Filters the attributes that do not need to be serialized, primarily to proxy and manage for hibernate.
* @param object property is @param Name Property name * @param Value property values * @return return False property will be ignored and the Ture property will be reserved * * @Override public Boolean apply (Object object, String name, object value) {if (value instanceof hibernateproxy) {//hibernate
The object Lazyinitializer initializer = (hibernateproxy) value). Gethibernatelazyinitializer ();
if (initializer.isuninitialized ()) {return false; } else if (value instanceof persistentcollection) {//Entity association collection A one-to-many number of persistentcollection collection = (PERSISTENTC
ollection) value;
if (!collection.wasinitialized ()) {return false; Object val = collection.getvalue();
if (val = = null) {return false;
} return true; }
}
Of course, you can continue to expand, add constructors, configure, specify class, and which properties of this class need to be filtered. More refined control. Back again.
When called, you can declare a simplepropertyfilter by using the following method
@RequestMapping ("/json") public void test (HttpServletRequest request, httpservletresponse response) {Tags tags = ta
Gsdaoimpl.get (2);
Tags parenttags = tagsdaoimpl.get (1);
Tags.setparenttags (Parenttags);
Long d = system.nanotime ();
Simplepropertyfilter filter = new Simplepropertyfilter ();
String JSON = json.tojsonstring (tags, filter);
System.out.println (System.nanotime ()-D);
Objectmapper mapper = new Objectmapper ();
Mapper.registermodule (New Hibernate4module ());
Mapper.setserializationinclusion (Include.non_null);
Long D2 = System.nanotime ();
String json2 = null;
try {json2 = mapper.writevalueasstring (tags);
catch (Jsonprocessingexception e) {} System.out.println (System.nanotime ()-D2);
SYSTEM.OUT.PRINTLN (JSON);
System.out.println (Json2); }
The above code also shows that if you use Jackson, where you use Jackson2 and want to serialize hibernate agents and deferred loading objects, you need to introduce hibernate4module. Maven since the following
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId> jackson-datatype-hibernate4</artifactid>
<version>2.2.3</version>
</dependency >
absolutely original, all rights reserved. Reprint please indicate the source.