tags:

views:

23

answers:

1

I have these 2 mappings:

<hibernate-mapping>
    <class name="sample.Operator" table="OPERATOR">
        <id name="id" >
            <generator class="native" />
        </id>
        <property name="name"  not-null="true">
            <column name="NAME" />
        </property>
        <set name="pointCodes" inverse="false" lazy="true" cascade="save-update">
            <key>
                <column name="OperatorID" />
            </key>
            <one-to-many class="sample.PointCode" />
        </set>
    </class>

<hibernate-mapping>
 <class name="sample.PointCode" table="POINTCODE">
  <id name="id">
   <generator class="native" />
  </id>
  <properties name="pointCodeKey" unique="true">
   <property name="pointCode" not-null="true">
   </property>
   <property name="networkIndicator" not-null="true">
   </property>
  </properties>
  <property name="name" not-null="true">
  </property>
 </class>
  </hibernate-mapping>

Most of the time when featching an Operator I want the pointCodes to be fetched lazily, so I don't want to set lazy="false" in the mappings,

However I have one query, e.g. session.createQuery("from Operator").list() where I do want the pointCodes association to NOT be fetched lazily - how do I do that ?

+1  A: 

The hibernate reference manual writes:

A "fetch" join allows associations or collections of values to be initialized along with their parent objects using a single select. This is particularly useful in the case of a collection. It effectively overrides the outer join and lazy declarations of the mapping file for associations and collections. See Section 20.1, “Fetching strategies” for more information.

from Cat as cat
    inner join fetch cat.mate
    left join fetch cat.kittens

A fetch join does not usually need to assign an alias, because the associated objects should not be used in the where clause (or any other clause). The associated objects are also not returned directly in the query results. Instead, they may be accessed via the parent object. The only reason you might need an alias is if you are recursively join fetching a further collection:

from Cat as cat
    inner join fetch cat.mate
    left join fetch cat.kittens child
    left join fetch child.kittens

The fetch construct cannot be used in queries called using iterate() (though scroll() can be used). Fetch should be used together with setMaxResults() or setFirstResult(), as these operations are based on the result rows which usually contain duplicates for eager collection fetching, hence, the number of rows is not what you would expect. Fetch should also not be used together with impromptu with condition. It is possible to create a cartesian product by join fetching more than one collection in a query, so take care in this case. Join fetching multiple collection roles can produce unexpected results for bag mappings, so user discretion is advised when formulating queries in this case. Finally, note that full join fetch and right join fetch are not meaningful.

meriton