Five ‘s Weblog

November 9, 2007

Eager loading in Hibernate

Filed under: ORM — by powerdream5 @ 5:50 pm
Tags: , , ,

      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.

11_9_2_2007.jpg

About these ads

17 Comments »

  1. 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 |Reply

  2. [...] 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 |Reply

  3. 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 |Reply

  4. 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 |Reply

  5. I really do believe all of the concepts you’ve offered within your post. They’re really
    convincing and may certainly work. Still, the posts are
    very short for beginners. May just you please prolong them
    somewhat from next time? Thanks in your post.

    Comment by Lynell — May 28, 2013 @ 8:36 am |Reply

  6. There are several features of the Zygor guide which makes the
    guide a favorite of many. A good leveling guide will be constantly updated, so you can stay
    at the top of your game. You can put together scrap and used materials to make new candles.

    Comment by zygor guides de graça — May 28, 2013 @ 10:59 pm |Reply

  7. I appreciate this post, enjoyed it thanks for adding.

    Comment by home improvement — June 6, 2013 @ 10:41 pm |Reply

  8. Have you ever considered about including a little
    bit more than just your articles? I mean, what you say
    is valuable and everything. But think about if you added some
    great photos or videos to give your posts more, “pop”!

    Your content is excellent but with pics and video clips, this
    blog could undeniably be one of the best in its niche. Amazing blog!

    Comment by tablettes graphiques — July 1, 2013 @ 9:09 pm |Reply

  9. With havin so much content do you ever run into any problems of plagorism or copyright violation?
    My site has a lot of completely unique content I’ve either created myself or outsourced but it looks like a lot of it is popping it up all over the internet without my permission. Do you know any techniques to help reduce content from being ripped off? I’d definitely appreciate it.

    Comment by http://lovedelusions.com — July 27, 2013 @ 5:14 pm |Reply

  10. Hi, Neat post. There is a problem together with your web site
    in internet explorer, may check this? IE nonetheless is the marketplace leader
    and a good element of people will pass over your fantastic writing because of
    this problem.

    Comment by Grace — July 27, 2013 @ 11:30 pm |Reply

  11. That is very attention-grabbing, You are an excessively professional blogger.
    I have joined your feed and look forward to in the
    hunt for more of your great post. Additionally, I have shared your website in my social networks

    Comment by kitchen supplies — August 1, 2013 @ 11:27 am |Reply

  12. Most of the people use lucky dates as their birthdays and anniversaries to hit the jackpot.
    Although relatively simple to play the general complexity
    and structure of many of these types of games enables gamers to gain and enhance skills that will in fact be helpful within their work and academic lives.
    As we speak, there are three major gamers within the video game system industry.
    There are varieties of online games that are available in World Wide Web.
    Online driving games are here to entertain you because this game will really entertain you and
    will enhance you skill, techniques and alertness while driving
    in the game. In 1969, the enchantment started with
    the development of two players section and up to some extent that was education oriented.

    Comment by online games — August 5, 2013 @ 6:23 pm |Reply

  13. In this way, the hydroxyl radical is sort of nature’s atmospheric “scrubbing bubbles. Keep this plant away from pets and small children, as it is toxic if eaten. Instead, these products seem to be nothing more than fancy scented oil diffusers.

    Comment by mozilla.org — September 9, 2013 @ 4:20 pm |Reply

  14. Classic instance is good for the actual visitor within the house, they’re always furnished with coffee first.
    It would have been a luxury before, as I would have to go to the local Starbucks to get one.
    Coffee is something which is enjoyed by everyone since it has the quality to
    awaken your senses and make you feel refreshed.

    Comment by dooid.me — September 10, 2013 @ 11:18 am |Reply

  15. Well, needless to say I had to discover a solution to this particular problem.
    While a Phillips head will work well enough, a square drive, or
    Robertson head, will help to prevent the stripping in the screw head and maybe damaging the wood.
    With this sound enhancement, you get to enjoy mobile virtual
    multichannel audio.

    Comment by Werner — September 19, 2013 @ 5:50 pm |Reply

  16. Can you please provide some documentation/reference for below statement:

    “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”

    Comment by madhu — January 22, 2014 @ 7:19 am |Reply

  17. Thanks for another wonderful article. Where else could anybody get that kind of information in such an ideal way of writing? I have a presentation next week, and I’m on the look for such information. dfkbfeceakebadcd

    Comment by Smithk5 — April 29, 2014 @ 9:03 pm |Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Toni Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: