Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between Hibernate.initialize and eager fetching

Tags:

orm

hibernate

Why I can't use EAGER fetching for the same purpose

like image 665
sab Avatar asked Oct 01 '10 03:10

sab


People also ask

What is eager fetching in Hibernate?

EAGER fetching tells Hibernate to get the related entities with the initial query. This can be very efficient because all entities are fetched with only one query. But in most cases it just creates a huge overhead because you select entities you don't need in your use case. You can prevent this with FetchType.

What is the difference between eager and lazy fetching Hibernate?

Eager Loading is a design pattern in which data initialization occurs on the spot. Lazy Loading is a design pattern that we use to defer initialization of an object as long as it's possible.

What are the different fetching ways in Hibernate?

Hibernate defines the following fetching strategies: Join fetching: Hibernate retrieves the associated instance or collection in the same SELECT , using an OUTER JOIN . Select fetching: a second SELECT is used to retrieve the associated entity or collection.

What is the difference between the FetchType lazy and FetchType eager?

Enum FetchType. Defines strategies for fetching data from the database. The EAGER strategy is a requirement on the persistence provider runtime that data must be eagerly fetched. The LAZY strategy is a hint to the persistence provider runtime that data should be fetched lazily when it is first accessed.


1 Answers

You can use EAGER fetching to obtain the same result, at the end. But this means things will be always eagerly loaded (vs when you just need to). Is this what you want?

References

  • Hibernate Core Reference Guide
    • 20.1.4. Initializing collections and proxies
like image 63
Pascal Thivent Avatar answered Nov 13 '22 11:11

Pascal Thivent