I believe there is only two ways of loading objects using Hibernate and that is lazy loading and one is eager loading. Lazy loading has its own advantages, it is not loading lots of objects but only when you need them. I also have learned that if you want to force to load all the children for an object you can simply call the parent.getChildren().size()
. So let's say we have the following objects
@Entity
public class Customer{
public Set<Order> order;
}
@Entity
public class Order{
}
let's assume we have customers who has orders in our system and it might be more than one or even null. So my question is isn't it better to always using eager loading in this case? we need the size or some information for the order related to the customer. What is the benefit of using lazy loading in this situation, is there any benefits?
I am trying to understand where to use lazy loading and where to use eager loading, highly appreciate your insight.