Lazy loading of hibernate

Source: Internet
Author: User

Lazy loading of hibernate
Cause:

When you use hibernate to query an object, if the Session is closed and you call the set or object associated with the object, a lazy loading exception will occur!

 

Solution:

Solution 1:

Before the Session is closed, query the set or object associated with the object, and add all the methods at the business layer:

1 public ElecUser findUserByLogonName (String name) {2 3 String condition = "and o. logonName =? "; 4 5 Object [] params = {name}; 6 7 List <ElecUser> list = elecUserDao. findCollectionByConditionNoPage (condition, params, null); 8 9 // This user exists in the Database Table, returns the ElecUser object 10 11 ElecUser elecUser = null; 12 13 if (list! = Null & list. size ()> 0) {14 15 elecUser = list. get (0); 16 17} 18 19/*** 20 21 * solve the lazy loading exception 22 23 attributes other than OID 24 25 */26 27 elecUser. getElecRoles (). size (); 28 29 return elecUser; 30 31}View Code

 

Solution 2: Initialize the set or object associated with the object in the Service-layer method (before the Session is closed)

1 public ElecUser findUserByLogonName (String name) {2 3 String condition = "and o. logonName =? "; 4 5 Object [] params = {name}; 6 7 List <ElecUser> list = elecUserDao. findCollectionByConditionNoPage (condition, params, null); 8 9 // This user exists in the Database Table, returns the ElecUser object 10 11 ElecUser elecUser = null; 12 13 if (list! = Null & list. size ()> 0) {14 15 elecUser = list. get (0); 16 17} 18 19/*** 20 21 * solve the lazy loading exception 22 23 */24 25 Hibernate. initialize (elecUser. getElecRoles (); 26 27 return elecUser; 28 29}View Code

 

 

Solution 3: In ElecUser. hbm. xml, add lazy = "false". When querying a user, immediately search and query the role set associated with the user:

1 <set name = "elecRoles" table = "elec_user_role" inverse = "true" lazy = "false"> 2 3 <key> 4 5 <column name = "userID"> </column> 6 7 </key> 8 9 <strong-to-class = "cn. itcast. elec. domain. elecRole "column =" roleID "/> 10 11 </set>View Code

Indicates that when a user is queried, the user's associated roles are retrieved immediately.

We recommend that you do not add too many lazy = false values to. hbm. xml during project development. In this way, if there are many table associations and no query objects are loaded, performance problems may occur.

 

Solution 4: Use the OpenSessionInViewFilter provided by spring to add the filter to the web container.

Add the following in web. xml:

Requirement: the filter must be placed in front of the strtus2 filter and executed first.

1 <! -- Add the filter provided by spring, solve the Problem of hibernate lazy loading --> 2 3 <filter> 4 5 <filter-name> OpenSessionInViewFilter </filter-name> 6 7 <filter-class> 8 9 org. springframework. orm. hibernate3.support. openSessionInViewFilter10 11 </filter-class> 12 13 </filter> 14 15 <filter-mapping> 16 17 <filter-name> OpenSessionInViewFilter </filter-name> 18 19 <url- pattern> *. do </url-pattern> 20 21 <url-pattern> *. jsp </url-pattern> 22 23 </filter- Mapping> 24 25 <! -- Configure the struts2 filter, this is the core of struts2 running --> 26 27 <filter> 28 29 <filter-name> struts2 </filter-name> 30 31 <filter-class> 32 33 org. apache. struts2.dispatcher. ng. filter. strutsPrepareAndExecuteFilter34 35 </filter-class> 36 37 </filter> 38 39 <filter-mapping> 40 41 <filter-name> struts2 </filter-name> 42 43 <url- pattern> *. do </url-pattern> 44 45 <url-pattern> *. jsp </url-pattern> 46 47 </filter-mapping>View Code

Principles of OpenSessionInViewFilter:

1: Transaction commit:SpringThe declarative transaction control is still processed at the business layer. After the method is executed, the transaction is automatically committed. If an exception occurs, the transaction will be rolled back. But it is delayed.SessionClosing time.

2:SessionClose:SessionClose the page. When the data on the page is loaded, close it again.Session.

Problem:

If the system you developed loads large page data, OpenSessionInViewFilter is not suitable. In this way, the Session cannot be closed in time, and the other Session cannot be accessed and the connection is insufficient, it will generate a "false death" phenomenon.

 

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.