In default, Hibernate adopts the mechanism of lazy loading to load objects from databases. Lazy loading contributes a lot to improve the performance of an application by limiting the amount of objects that will be needed. In contrast to lazy loading, eager loading loads the full objects tree once. Because eager loads multiple related objects with a single SELECT statement instead of using multiple SELECT statements, therefore in some situations, eager loads can also improve the performance of the application by reducing the times of accessing the database.
There are two ways to configure eager loading in Hibernate. They are staticly eager loading and dynamically eager loading respectively. If the application always traverses a relationship, you might want to configure it to be always staticly eager loaded. Conversely, you might want to dynamically configure eager loading for relationships that are only traversed by the application when handling particular requests.
One way to configure static eager loading is specifying a value for the fetch attribute of the relationship’s mapping element.
This attribute can have one of two values:
select: Lazily load the referenced object or collection with a separate SQL SELECT statement. It is the default value
join: Eagerly load the referenced object or collection using an outer join
<hibernate-mapping>
<class name=”?” table=”?”>
<many-to-one name=”?” fetch=”join” column=”?” />
</class>
</hibernate-mapping>
To configure dynamical eager loading, you must use queries with fetch joins.
<hibernate-mapping>
…
<query name=”queryName”>
<![CDATA[
from student s
left outer join fetch s.classID as c
left outer join fetch c.className
where s.id = ?
]]>
</query>
….
</hibernate-mapping>
Finally, take a note that Hibernate has a rule that a class can only have at most one collection loaded using an outer join. This is to prevent inefficient queries that return the Cartesian product of too large collections.






Hi,
I have used hibernate with spring. Lot of hibernate queries are getting logged. I didn’t give ant refernce to lazy i.e. it will take default value. Will not the immediate and eager fetching create lot of queries?
Comment by Sen — December 27, 2007 @ 6:13 am |
[...] If object A has relation with object B, if you load object A, object B is not loaded from db automatically; it will be loaded only when you do getB(). Eager fetch is the opposite technique. In eager fetch B is loaded when A is loaded if A contains B. The default mechanism is lazy loading.When to use lazy loading and when to use eager fetch:Lazy load will not automatically load child objects. It will load child objects only when getChild() is called. Eager fetch will automatically load child objects when the parent object is loaded.Whether to use lazy load or eager fetch depends on your performance requirements. If you use eager fetch children were already loaded and are already in memory so accessing these objects will be faster. Memory consumption will be more because more objects are in memory even if they are not needed.If you use lazy load, child objects are loaded only when required (when getter method is invoked). Hence accessing child objects will be somewhat slower but will use lesser memory since objects are loaded only if they are required.http://powerdream5.wordpress.com/2007/11/09/eager-loading-in-hibernate/ [...]
Pingback by The long and short of it all « Tips from a mentor to his protégé — March 21, 2012 @ 5:56 am |
Social networking sites like Twitter, Facebook, My – Space, Digg, etc.
Businesses expand their presence by getting access to the
web hosting services. Many businesses initially build or buy an in-house mail server in
the interest of protecting their correspondence.
Comment by please click the following webpage — May 6, 2013 @ 4:15 pm |
The patent on farmacia on line in China, making it difficult to shoot a variety of reasons.
If you want to distribute these things to other parties in an organized manner, tough luck.
In late winter it is forbidden to knit on the doorstep, as that is known around the world who did not use either drug.
Comment by what is it worth — May 11, 2013 @ 11:07 pm |