This article summarizes four ways to solve the problem of lazy loading of hibernate in the process of learning hibernate.
The so-called lazy loading is delayed loading, delayed loading.
When to use lazy loading? I can only answer that when you want to use lazy loading, use lazy loading.
As for why we need to use lazy loading, it is obvious that when the amount of data we want to access is too large, it is obviously not suitable to use cache, because the memory capacity is limited. In order to reduce the amount of concurrency and reduce the consumption of system resources, we let the data be loaded when needed, and then we use lazy loading.
For example, there is an object that is Employee and another object that is Department. Obviously, for Employee, it is a many-to-one relationship compared to Department, and for Department, it is a one-to-many relationship compared to Employee. When we query the Employee object, if we want to query the corresponding Department through the property department of the employee object, an exception will be thrown. This is because of the existence of lazy loading. After the session is closed, hibernate issues another request to the database, and an exception is thrown.
The following are four ways to solve this problem:
1. Explicit initialization (inside the query method)
When asking which department an employee belongs to, you need to pre-query the Department
Use statement
Hibernate.initialize(Department.class);
2. Modify the object relationship file and rewrite lazy lazy=false, that is, close lazy loading
The above two methods can indeed solve the problem, but the disadvantage is that no matter whether the object is used later, hibernate will issue SQL statements to the database to request data, causing unnecessary performance waste.
3. Use filters (web project)
①The way to get session must use getCurrentSession
②Special closing 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);//Requests are constantly going tx.commit(); } catch (Exception e) { // TODO: handle exception if(tx != null){ tx.rollback(); } } finally{ //Special closing method HibernateUtil.closeCurrentSession(); } }4. In the SSH framework, use the openSessionView provided by spring
The principle is similar to the use of Filter in the third method, except that this filter is provided by spring. When using it, you only need to configure it in the web.xml file as follows:
<!-- Use spring to solve lazy loading problems --> <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>
The methods in 3 and 4 can also solve the problem of lazy loading, and the fourth method is also used more frequently at present. However, these two methods also have disadvantages. The disadvantage is that they prolong the time when the session is closed and the life cycle of the session becomes longer. Before using this method, the session was closed after querying data; now, the session is closed at the end of a web request.
Summarize
The above is all about the four ways to solve the problem of lazy loading of Hibernate. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!