If a cascading query occurs in Hibernate, there may be lazy loading problems. For example, I now have an Account (administrator) class, Category (product category) and Product (product) class. From left to right, it is a one-to-many relationship, and from right to left, @ManyToOne (fetch=FetchType.LAZY) is set. I now want to find out the product information and package it into json format to pass it to the front desk. I use the query statement in the background as:
from Product p left join fetch p.category where p.name like:name
This way, you can find out the Product, and then the Category in Product is also put in. However, the Account in Category is not an actual object, but a temporary proxy object. This is easy to understand, because I checked Product and only cascaded Category. As for Category and Account, it is configured according to the actual (LAZY).
Now put the query product into the map, and then convert it to json format and return to the front desk, there will definitely be a lazy loading problem, because the Account object will be taken during the process of converting json, but the session has been closed at this time, so an error will be reported. A very direct but not very good solution is to change the LAZY in Category to EAGER, so that the Account information can be found, but this is not good. So we use another method: set a blacklist in struts.xml, and use regular expressions to filter out the account in category when converting to json format, so we will not check the account object, and there will be no laziness loading problem. as follows:
At this point, there should be no problem. However, in my project, I still report lazy loading exceptions, which means that it doesn't work after I configure this way. But theoretically, after configuration, it will be OK, and the data can be packaged into json format and passed to the front desk normally. This problem bothered me for two days, so I simply changed LAZY to EAGER and started to do the project.
Today I contacted the exception here in another Hibernate exception and solved it! Today in Hibernate, I want to call the get method to get the product information, but I cannot get it. There is no message on the background console. Since I turned on the dev mode, the front desk displayed the error message:
java.lang.ClassCastException:cn.it.shop.model.Product_$$_javassist_0 cannot be cast to javassist.util.proxy.Proxy</span>
Can't be converted to a proxy? ? Why change to an agent? Generally, aren't the agents unable to be converted into actual objects? So I searched the Internet and found that this problem may be due to a javassist jar package in the project that conflicts. I went to the project to check it out, and it turned out to be true:
It really conflicts... So I just deleted the javassist-3.11.0.GA.jar in the struts package. Hibernate is correct, and you can get the product information normally. Then I remembered the problem of struts2 switching to json 2 days ago, so I went back to change EAGER back to LAZY. The problem was gone and I could also convert to json normally. I was depressed. It was really caused by the conflict between jar packages. Because there was no error at that time, but I couldn't find the returned json data on the front desk. I only knew that the json data was not returned. It must be a problem with the background transfer to json. According to existing experience, 90% of it was lazy loading, but I didn't expect it to be caused by the jar package conflict.
Later on: If the jar package does not conflict but cannot convert json, it is basically a problem caused by lazy loading. The method of filtering out lazy loading objects by configuring blacklists in struts.xml is very practical. There is no need to modify the configuration in POJO. I will transfer which fields I want to transfer to json, and if I don’t want to, it is very convenient.
Original link: http://blog.csdn.net/eson_15/article/details/51394302
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.