Lazy associations – IV
31 August 2008In 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.
Related Posts:
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