About lazy loading problems with hibernate

Source: Internet
Author: User

Why do I need to delay loading?

Because it will improve efficiency, the existence is reasonable, haha. General settings lazy loading is on the Object relational map (on many sides). For example:

A user has multiple consumption records.

Set to lazy, when querying the data will only take out the consumption record (more than one side), do not query the user (this does not improve efficiency, that is, the association data will not query the database), but another problem, I want to query the database when the associated data query out how to do?

The associated data is queried once more when the query is needed.

This is what Baidu wrote:

if it is eager, then the data associated with it is taken out and put in memory at the same time. (all data are queried at once)

If it is lazy, then when the data is taken out, its associated data is not taken out, in the same session, when to use, when to fetch (again access to the database).

Popular Science:

1, Fetchtype.lazy: Lazy loading, loading an entity, the definition of lazy loaded properties are not immediately loaded from the database. 2, Fetchtype.eager: Urgent load, when loading an entity, the properties that define the urgent load are immediately loaded from the database. 3, for example, the user class has two attributes, name and address, like Baidu know, login after the user name is required to display, this attribute is used to the odds are great, to immediately to the database to check, with the urgent load; and the user address in most cases do not need to display, only in the viewing user information is needed to display, Need to use to check the database, with lazy loading just fine. Therefore, not a login to the user's all the data loaded into the object, so there are two load modes.
Summary: When there is a relational object mapping, you need to set a lazy load (more than one side). can improve efficiency.

About lazy loading problems with hibernate

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.