I’m somewhat new to NHibernate and I’ve run into a problem trying to join two tables. I have a name table and address table. I want to pull the name record regardless if any results are returned for the address record. If I have an address record in the code below works, but once the address record is removed I no longer receive the name record. I was trying this (NHibernate Left Outer Join), but it doesn’t work for me. Any ideas?
Mapping:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping
xmlns="urn:nhibernate-mapping-2.2"
namespace="Portlet.IncomingStudentInfo.Data.BusinessObjects"
assembly="Portlet.IncomingStudentInfo">
<class name="ISINameMasterRecord" table="NAME_MASTER">
<id name="NM_ID_NUM" column="ID_NUM" type="Int32">
<generator class="native" />
</id>
<property name="NM_ID_NUM" column="ID_NUM" />
<property name="NM_EMAIL_ADDRESS" column="EMAIL_ADDRESS" />
<property name="NM_MOBILE_PHONE" column="MOBILE_PHONE" />
<many-to-one name="LHP" class="ISILHPAddress"
column="ID_NUM" fetch="join"
foreign-key="ID_NUM"
outer-join="true" not-found="ignore" />
</class>
<class name="ISILHPAddress" table="ADDRESS_MASTER">
<composite-id>
<key-property name="AD_ID_NUM" column="ID_NUM" type="Int32" />
</composite-id>
<property name="AD_ID_NUM" column="ID_NUM" />
<property name="AD_ADDR_CDE" column="ADDR_CDE" />
<property name="AD_ADDRESS" column="ADDR_LINE_1" />
<property name="AD_CITY" column="CITY" />
<property name="AD_STATE" column="STATE" />
<property name="AD_ZIP" column="ZIP" />
<property name="AD_PHONE" column="PHONE" />
</class>
</hibernate-mapping>
Facade:
public class ISINameMasterRecordFacade : JICSBaseFacade<ISINameMasterRecord>
{
public ISINameMasterRecord FindIDCriteria(int id)
{
ICriteria criteria = this.CreateCriteria();
criteria.Add(Expression.Eq("NM_ID_NUM", id));
criteria.CreateAlias(
"LHP",
"lhp",
NHibernate.SqlCommand.JoinType.LeftOuterJoin);
criteria.Add(
Expression.Or(
Expression.IsNull("lhp.AD_ADDR_CDE"),
Expression.Eq("lhp.AD_ADDR_CDE", "*LHP")));
return criteria.UniqueResult<ISINameMasterRecord>();
}
}
Thanks for everyone’s help on this. I was speaking with another developer and he provided the following answer, which does work. Here’s some additional information on the issue. The NAME_MASTER table holds the name information for the record with the primary key being ID_NUM. The ADDRESS_MASTER table holds all the addresses for a record with the primary keys being ID_NUM and ADDR_CDE (they type of address it is: legal home permanent, email, summer address, etc.) A record may not have a *LHP (legal home permanent address), but may have other address records. We wanted to pull the NAME_MASTER record regardless of the presence of the *LHP address record, so this is really another condition on the join. Below is working and will pull the NAME_MASTER record regardless of the presence of a *LHP record in ADDRESS_MASTER.
mappings.hbm.xml
ISINameMasterRecordFacade.cs
ISINameMasterRecord.cs
ISILHPAddress.cs