Hi:
I have the following hbm.xml file, which defines the relationship between AGENCY and AGENT tables.
Code:
<hibernate-mapping package="com.fpi.domain.impl">
<class name="AgencyImpl" table="AGENCY">
<id name="agencyId" column="AGENCYID" unsaved-value="null" type="integer">
<generator class="identity"/>
</id>
<property name="name" column="NAME" type="string"/>
<property name="deleted" column="DELETED" type="string"/>
<property name="sisId" column="SISID" type="string"/>
<set name="agents" table="AGENT" order-by="FIRSTNAME" outer-join="true">
<key column="AGENCYID"/>
<one-to-many class="AgentImpl"/>
</set>
</class>
</hibernate-mapping>
I need to retrieve only agencies and agents which are not deleted. I use the following call:
Code:
HibernateTemplate template = new HibernateTemplate(getSessionFactory());
return (List) template.execute(new HibernateCallback()
{
public Object doInHibernate(Session session) throws HibernateException
{
return (List) session.createCriteria(AgencyImpl.class)
.createAlias("agents", "agent")
.add(Expression.eq("this.deleted", "N"))
.add(Expression.eq("agent.deleted", "N"))
.list();
The query is generated correctly, but then agents are retrieved for the second time by hibernate. I tried setting lazy="true" on agents, but in this case no agents are attached to the agency at all.
What is the right way of doing it?