Defining fetch strategy

8 October 2008

To tune fetch strategy, you have various options. Which to choose depends on the scenario. You may define the fetch strategy in the mapping document. For instance:

<set name="permissions" 
            fetch="join">
    <key column="userId"/>
    <one-to-many class="Permission"/>
</set
<many-to-one name="mother" class="Cat" fetch="join"/>

This will work but usually mapping document in not a nice place to define the fetch strategy. You may define the fetch strategy in HQL queries or in Criteria queries. This actually overrides the default behavior defined in the mapping document.

If you wish to define fetch strategy in HQL, use left join fetch in HQL. This tells Hibernate to fetch the association eagerly in the first select, using an outer join. In the Criteria query API, you would use setFetchMode(FetchMode.JOIN).

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

del.icio.us:Defining fetch strategy  digg:Defining fetch strategy  spurl:Defining fetch strategy  wists:Defining fetch strategy  simpy:Defining fetch strategy  newsvine:Defining fetch strategy  blinklist:Defining fetch strategy  furl:Defining fetch strategy  reddit:Defining fetch strategy  fark:Defining fetch strategy  blogmarks:Defining fetch strategy  Y!:Defining fetch strategy  smarking:Defining fetch strategy  magnolia:Defining fetch strategy  segnalo:Defining fetch strategy  gifttagging:Defining fetch strategy

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: Defining fetch strategy

Leave a Reply