Struts2 The lazy load exception problem in the package JSON format _java

Source: Internet
Author: User
Tags pack package json

If cascading queries occur in Hibernate, lazy loading problems may occur, such as I now have the account (Admin) class, Category (Commodity category) and product (product) class, from left to right are a one-to-many relationship, and the @manytoone (Fetch=fetchtype.lazy) is set from right to left. I am now going to find out the product information to pack into the JSON format to the front desk, I use the query in the background as follows:
From the Product P left join fetch p.category where P.name like:name
so you can check out the product, and then the product in the category also put in, but the category account is not the actual object, is a temporary proxy object, this very good understanding, Because I checked the product and only cascaded the category, as for category and account based on the actual configuration (LAZY).
Now put the query out of product into the map and then go back to the foreground in JSON format there is a certain lazy loading problem, because the account object will be taken in the process of turning JSON, but the session is closed so that the error occurs. A very direct but not very good solution is to change the lazy in the category to eager, so that the account information can also be found out, but it's not good. So we use another way: In the Struts.xml set a blacklist, in the JSON format when the use of regular expressions will be category in the account filter out, will not check the account object, there will be no lazy loading problems. As follows:

Here, there should be no problem. However, in my project I still report lazy loading exceptions, which means that I have not been configured to work at all. But in theory, this configuration is OK, you can normally package the data into a JSON format to the foreground. This problem bothered me for 2 days, and then simply changed the lazy to eager first, the project down.
Today, in another hibernate anomaly, I contacted the anomaly here and solved it! Hibernate today I want to call the Get method to obtain the product information, cannot get to, the console in the background does not have any messages, because I opened the dev mode, the foreground displays the error message:
Java.lang.classcastexception:cn.it.shop.model.product_$$_javassist_0 cannot be cast to Javassist.util.proxy.proxy</span>
Unable to convert to agent?? Why do you want to turn it into an agent? Generally not all agents can not be turned into the actual object? So I searched the Internet, this problem may be due to a javassist jar bag conflict in the project, I went to the project to check, sure enough:

It's really a conflict. Yo, hey ... So delete the struts package in that Javassist-3.11.0.ga.jar can, hibernate this right, you can normally take out merchandise information. Then I think of 2 days ago Struts2 turn to the question of JSON, so go back to the eager back to lazy, the problem is gone, also can normal turn to JSON, depressed, is really jar pack conflict caused trouble. Because there was no error, just the front desk I can't find the returned JSON data, only know did not return JSON data, it must be back to json out of the problem, according to the existing experience, 90% is lazy loading problem, but did not think is the jar pack conflict caused trouble.
Something: If the jar pack does not conflict, but cannot turn to JSON, it is basically lazy to load up the trouble, The use of struts.xml in the way of the list of lazy loaded object filtering method is very practical, do not modify the configuration in the Pojo, I want to change which fields to JSON to turn which, do not want to turn, very convenient.

Original link: http://blog.csdn.net/eson_15/article/details/51394302

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.