Reputation: 21338
Let's say we have one to many relationship between Customer and Phone..
class Customer{
@OneToMany(cascade = {CascadeType.ALL},mappedBy = "customer", fetch = FetchType.LAZY)
@Fetch( FetchMode.SELECT)
private List<Phone> phoneList;
}
In the above code, what is the difference between fetch = FetchType.LAZY and FetchMode.SELECT.
I read it that they both are same i.e. they both lazily loaded the underlying collection.
Can somebody explain me which one to use when?
Upvotes: 6
Views: 7659
Reputation: 403591
Fetch type (lazy/eager) refers to when Hibernate will fetch the association, whether in advance when it fetches the entity (eager), or whether it waits for the code to ask for the association (lazy).
Fetch mode (select/join) refers to how Hibernate will fetch the association, i.e. does it use an extra SELECT statement, or does it use a join.
Some combinations of these make no sense, e.g. lazy+join. If you use lazy fetching, then SELECT fetch mode is the only one that you can do.
If you use eager fetch, then you can choose to use either fetch mode.
Upvotes: 13