Lazy associations – IV

31 August 2008

In normal routine, we do not use the mapping document to customize fetching. What we do is, we keep the default behavior, and override it for a particular transaction,. This is done by using left join fetch in HQL. This actually tells Hibernate to fetch the association eagerly in the first select, using an outer join.

If you want to change the fetching strategy used by get() or load(), then use a
Criteria query, for example:

User user = (User) session.createCriteria(User.class)
.setFetchMode("permissions", FetchMode.JOIN)
.add( Restrictions.idEq(userId) )
.uniqueResult();

This is s way to avoid problems with N+1 selects is to use the second-level cache.

del.icio.us:Lazy associations – IV  digg:Lazy associations – IV  spurl:Lazy associations – IV  wists:Lazy associations – IV  simpy:Lazy associations – IV  newsvine:Lazy associations – IV  blinklist:Lazy associations – IV  furl:Lazy associations – IV  reddit:Lazy associations – IV  fark:Lazy associations – IV  blogmarks:Lazy associations – IV  Y!:Lazy associations – IV  smarking:Lazy associations – IV  magnolia:Lazy associations – IV  segnalo:Lazy associations – IV  gifttagging:Lazy associations – IV

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Lazy associations – IV

Leave a Reply