English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

A Brief Discussion on Solving Hibernate Lazy Loading4This way

This article summarizes four methods I have used to solve the lazy loading problem in the process of learning Hibernate.

Lazy loading (lazy) is essentially a delayed loading, a deferred loading.

When should we use lazy loading? I can only answer that we should use lazy loading when it is necessary.

As for why we use lazy loading, it is because when the amount of data we want to access is too large, it is obviously not suitable to use caching, as memory capacity is limited. In order to reduce the amount of concurrent access and save system resources, we load the data only when it is needed, which is when we use lazy loading.

For example, there is an object called Employee, and another object called Department. It is obvious that for Employee relative to Department, it is a many-to-one relationship; while for Department relative to Employee, it is a one-to-many relationship. When querying the Employee object, if you want to query the corresponding Department through the attribute department of the employee object, an exception will be thrown. This is because of lazy loading, after the session is closed, Hibernate sends a request to the database again, resulting in an exception.

The following summarizes four ways to solve this problem:

1.Explicit initialization (within the query method)

When querying which department an employee belongs to, a preliminary query of Department is required

Use the statement

Hibernate.initialize(Department.class); 

2.Modify the object relationship file, rewrite lazy to lazy=false, that is, turn off lazy loading

These two methods can indeed solve the problem, but the disadvantage is that regardless of whether the object is used later, Hibernate will send SQL statements to the database to request data, causing unnecessary performance waste.

3.Using a filter (web project)

①The method to obtain the session must use getCurrentSession

②Special shutdown session method

public void doFilter(ServletRequest request, ServletResponse response, 
   FilterChain arg2) throws IOException, ServletException { 
  // TODO Auto-generated method stub 
  Session session = null; 
  Transaction tx = null; 
  try { 
   session = HibernateUtil.getCurrentSession(); 
   tx = session.beginTransaction(); 
   arg2.doFilter(request, response);//The request is always running 
   tx.commit(); 
  } 
   // TODO: handle exception 
   if(tx != null){ 
    tx.rollback(); 
   } 
  } 
   //Special shutdown method 
   HibernateUtil.closeCurrentSession(); 
  } 
 } 

4.In the SSH framework, use the openSessionInView provided by Spring

The principle is similar to the third method using Filter, but this filter is provided by Spring. When using it, you only need to configure as follows in the web.xml file:

<!-- Using Spring to Solve Lazy Loading Issues --> 
 <filter> 
  <filter-name>OpenSessionInViewFilter</filter-name> 
  <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> 
 </filter> 
 <filter-mapping> 
  <filter-name>OpenSessionInViewFilter</filter-name> 
  <url-pattern>/*</url-pattern> 
 </filter-mapping> 

the3and the4methods can also solve the lazy loading problem, among which the4These methods are also currently used more. However, both methods also have drawbacks, namely, they extend the time of session closure, and the session lifecycle becomes longer. Before using this method, the session was closed after the data was queried; now, the session closure is at the end of a web request.

Summary

That is all about the discussion of how to solve Hibernate lazy loading in this article4All the content of this type, hoping it will be helpful to everyone. Those who are interested can continue to read other related topics on this site, and welcome to leave comments if there are any shortcomings. Thank you for your support to this site!

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)

You May Also Like