Four ways to solve hibernate lazy loading

This paper summarizes four ways to solve the problem of Hibernate lazy loading 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 lazy loading is used when you want to use lazy loading.

As for why lazy loading is used, it is obviously inappropriate to use cache when the amount of data we want to access is too large, 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 necessary. At this time, we use lazy loading.

For example, one object is employee and another object is department. Obviously, the relationship between employee and department is many to one; The Department is a one to many relationship with the 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 sends another request to the database, and an exception is thrown.

Here are four ways to solve this problem:

1. Explicit initialization (inside the query method)

To query which department an employee belongs to, you need to query the Department in advance

Use statement

2. Modify the object relationship file and change lazy to lazy = false, that is, close lazy loading

The above two methods can indeed solve the problem, but the disadvantage is that whether the object is used later or not, hibernate will send SQL statements to the database to request data, resulting in unnecessary performance waste.

3. Use filters (web projects)

① The way to get a session must use getcurrentsession

② Special closing session mode

4. In the SSH framework, use the opensessionview provided by spring

The principle is similar to the filter used in the third method, except that the filter is provided by spring. You only need to use it on the web The XML file configuration is as follows:

Methods 3 and 4 can also solve the problem of lazy loading, and the fourth method is also widely used at present. However, these two methods also have disadvantages. The disadvantage is that the closing time of the session is prolonged and the life cycle of the session becomes longer. Before using this method, the session is closed after querying the data; Now, the session is closed at the end of a web request.

summary

The above is all about the four ways to solve hibernate lazy loading in this paper. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>