When to use Lazy load / Eager loading in sleep mode?

I believe that there are only two ways to load objects using Hibernate, and that is lazy loading, and one is loaded. Lazy loading has its advantages; it does not load many objects, but only when you need it. I also found out that if you want to force download all child objects for an object, you can just call parent.getChildren().size() . So, let's say we have the following objects

 @Entity public class Customer{ public Set<Order> order; } @Entity public class Order{ } 

Suppose we have customers who have orders in our system, and may be more than one or even zero. So my question is, is it not always better to use active boot in this case? we need size or some information for customer related order. What is the advantage of using lazy loading in this situation, are there any advantages?

I'm trying to figure out where to use lazy loading and where to use impatient loading, appreciate your insight.

+7
source share
3 answers

I am trying to figure out where to use lazy loading and where to use look forward to downloading, appreciate your understanding.

Here are a few thoughts:

1) If you are going to always use something (for sure), you can download it.
2) Associated with 1, if you are almost never going to use something, lazy loading.
3) Lazy loading is usually more useful when large collections are involved.
4) A terrible load of things will reduce session-related errors at the potential cost of a performance hit.
5) For complex data models and / or large databases, you will see how your application works under load, adjust your strategies. 6) It's hard to get it right the first time. Do what seems right and don't be afraid to change if necessary.
7) For large datasets, you will probably end up writing your own hql / queries, where default mappings can be overwritten, so lazy versus impatience will not make much difference.

If you believe # 6, then don't get stuck trying to plan too far ahead, and change it if you need to.

WRT is your specific example, I would probably write a bunch of requests for data access (of course, taking into account the relevant business needs)

1) A request that loads the client and leaves the orders in db (such a lazy load), which I would call when I need to get information about the client
2) A request that downloads the client and all the information about the order, in cases where I need it. Therefore, in this case, I will ignore the default mapping.

With these two requests in my service layers, I have tools that I need to do correctly, based on the context of the situation.

+20
source

This link answers your question perfectly.

LAZY loading is used in cases where the size of the linked object is huge, and on the other hand, it is not required to be extracted each time from the other side.

EAGER should be used with proper analysis because it loads relationships every time the main object is loaded.

So, if relationships are absolutely necessary for computing business logic, you should consider using the EAGER download; LAZY loading will serve most cases and provide less performance.

+5
source

LAZY is the default fetch type for all Hibernate annotation relationships. When you use the Lazy fetch type, Hibernate will not load relationships for this particular instance of the object. So what is the difference between Eager and Lazy?

The Eager extraction type, in fact, is the opposite of Lazy, by default, Eager loads ALL relationships associated with the specific object loaded by Hibernate. That means if you change your relationship to be that

enter image description here

Hibernate will now load the user profile into the default user object. This means that if you access user.getUserProfile (), it will not be NULL (unless the join table actually contains a matching row for the connection column user_profile_id).

So, a short story here:

FetchType.LAZY = Does not load relationships unless explicitly β€œrequested” through the retrieval method FetchType.EAGER = Loads ALL relationships.

Pros and Cons If using a delayed loading approach can throw NullPointerException, then why the hell would you want to use it? Ok, let's wait a minute to think about it.

Suppose we had a User object that has 10 different relationships with other objects. Let's also assume that you have a user who is trying to log in, and you just want to check if their username and password match what is stored in the database. Are you sure you want to download ALL 10 relationships and DO NOT use ANY of these downloaded relationships? Remember that loading things from the database is quite "expensive" in terms of the time required to load all this information into Java objects and store it in memory. And what happens if one of the relations points to an object that ALSO has 10 relations (and all of them are configured as the EAGER fetch type). Think of the cascading killing of objects loaded from a database into memory!

The way I would put it this way:

EAGER: comfortable but slow

Lazy: more coding, but much more efficient

+2
source

All Articles