Delay loading exceptions and solutions in Java programs

  • 2020-04-01 03:40:26
  • OfStack

The so-called lazy loading is lazy loading, lazy loading.

When do I load with lazy? I can only answer that when I load with lazy, I load with lazy.

As for why we use lazy loading, when the amount of data we want to access is too large, it is obviously not appropriate to use cache, because the memory capacity is limited, in order to reduce the amount of concurrency, reduce the consumption of system resources,

We use lazy loading when we let the data load only when needed. There are many kinds of lazily loaded exceptions. Are you referring to the exception whose session has been closed? Because that novice is most likely to encounter when you load a one-to-many relationship of this end of the object, and then after the transaction, such as on the page, also want to get the attributes of the multi-object in addition to the id, will run this exception.

Let me give you an example. You have a Grade object that is associated with a lot of Student objects and USES lazy loading

In the service, you get a Grade object through the database query, which contains a Set< Student> The students collection, but you didn't do anything further with the object (such as intialize or something like that to actually de-instantiate the lazy loaded proxy object)
Then when you get to the presentation of the page, go over the students collection, and you try to display the name property of each student on the page (which is not a studentId property) and you throw an exception that the session is closed

In the SSH framework:

You can use the OpenSessionInViewFilter directly
Configure in the configuration file


 <filter-name>opensession</filter-name>  
         <filter-class>       org.springframework.orm.hibernate3.support.OpenSessionInViewFilter  
       </filter-class>  
 <filter-mapping>  
      <filter-name>opensession</filter-name>  
    <url-pattern>*.action</url-pattern>
 </filter-mapping>

That's all for this article. I hope you enjoy it.


Related articles: