Hi:
I have 2 tables: user and agent. User table has agentId as a foreign key.
The association is defined as follows:
Code:
<hibernate-mapping package="com.acme.impl">
<class name="UserImpl" table="APPUSER">
<id name="userId" column="APPUSERID" unsaved-value="null" type="integer">
<generator class="identity"/>
</id>
<property name="login" column="userName" type="string"/>
<property name="lastName" column="lastName" type="string"/>
<property name="firstName" column="firstName" type="string"/>
<property name="lastLoggedOn" column="lastlogin" type="timestamp"/>
<property name="agentId" column="agentId" type="integer"/>
<many-to-one
name="agent"
class="AgentImpl"
column="AGENTID"
cascade="all"
unique="true"
</class>
</hibernate-mapping>
.
When a select is issued against the user table, I don't want a select issued against agent table, unless I specifically call getAgent(). To my understanding the following code should do the trick:
Code:
public UserImpl getUser(final Integer userId) throws Exception
{
HibernateTemplate template = new HibernateTemplate(getSessionFactory());
return (UserImpl) template.execute(new HibernateCallback()
{
public Object doInHibernate(Session session) throws HibernateException
{
return (UserImpl) session.createCriteria(UserImpl.class)
.add(Expression.eq("userId", userId))
[b].setFetchMode("agent", FetchMode.LAZY)[/b]
.uniqueResult();
}
}, false);
}
.
But for each call to the user table a call to the agent table is also made.
What am I doing wrong?
Thanks.