I am using Hibernate 3.6 with Spring 3.0.5.
I have the following mapping for a User object
<class name="foo.User" table="FOO_USER">
<id column="USER_ID" name="id" type="java.lang.Integer">
<generator class="identity"/>
</id>
<property name="firstName" column="FIRST_NAME" type="java.lang.String" length="100"/>
...
<many-to-one name="organization" column="ORGANIZATION_ID class="foo.Organization" not-null="true" update="false" />
...
The User has a many-to-one relationship with Organization. Usually, I want that relationship to be eagerly loaded, so the mapping sticks with the default setting of lazy=false (by not specifying anything).
There is a certain case where I do not want to eagerly load the Organization. I tried specifying this with a Criteria
(User)getSession().createCriteria(User.class)
.add(Restrictions.eq("id",id))
.setFetchMode("organization", FetchMode.SELECT)
.uniqueResult();
But the fetch mode is being ignored. Hibernate still eagerly loads the Organization relationship. I’ve been knocking my head against this for a few hours. Any help would be appreciated.
It’s always best to leave associations mapped as lazy and use fetching strategies to tune performance. I don’t believe there’s a way to map something as lazy and then make it un-lazy in a particular instance. Certainly fetch=select won’t do it, as that doesn’t imply anything about laziness. See section 21.1 of “Improving Performance” in the reference manual for an explanation of the concepts.