Fastjson serialization Hibernate proxy and deferred loading objects appear no session exception solution __js

Source: Internet
Author: User
Tags object object reserved serialization
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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.