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