Delayed loading of Hibernate

Source: Internet
Author: User

Delayed loading of Hibernate
I. Delayed loading and functions? Delayed loading is also called lazy loading. It means that the data is loaded only when it is used for data access and not loaded when it is not used. 2. What are the benefits of doing so? The advantage is that each time you access data, only the required data is loaded, which can reduce the pressure on the database and memory. Although the total data volume is not large sometimes, This is a mechanism. When the data volume is large, the optimization of the overall project or method call will be significant. 3. What are the differences between get and load methods in hibernate? 1. In hibernate, The get method is immediately loaded, that is, the data is immediately queried from the database, and the data is directly returned if there is data, if no corresponding data exists, null is returned. hibernate does not load the load method immediately. hibernate considers the data to be queried to exist, therefore, a proxy object is generated through CGLIB. The database is queried only when the attribute of the object is used. However, if the object does not exist in the database, an exception is thrown: ERROR LazyInitializer: 63-Exception initializing proxy (this Exception may also occur: when the session is closed and the attributes in the object are called, in general, this happens when the view layer calls the attributes of this object, because the session is closed at this time ). Therefore, the exception thrown by using the load method is not caused by the creation of an object, but by calling the attributes of this object. 2. cache is involved in queries: For the get method, hibernate first queries the level-1 cache session and then queries the level-2 Cache. If not, It queries the database, if there is still a database, null is returned. For the load method, hibernate first queries the level-1 cache session. If not, a proxy object is created, when the actual data is used, the second-level cache and database will be queried. Iv. Solution to delayed initialization errors: Cause of the error: delayed initialization errors are the most common errors in Hibernate development projects. If a delayed retrieval policy is configured for a class or set, it can be initialized only when the proxy class instance or proxy set is in the persistent state (that is, within the Session range. If it is initialized only when it is in the Free State, a delayed initialization error will occur. Solution: 1. Do not initialize the delayed loading object. If it is an object property, you can ignore initialization: configuration: Add Annotation on the set Method of this property: @ JSON (serialize = false) 2. Configure the delayed loading attribute in the configuration to load immediately: lazy = false, and the default value of the lazy attribute is true. 3. initialize the delayed object before the service layer session is closed. 4. OpeanSessionView configuration: OpenSessionInViewFilter enables the session to the presentation layer. The configuration is as follows:

<! -- OpenSessionInViewFilter must be configured before Struts2 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>

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.