我对 NHibernate 有点陌生,我在尝试加入两个表时遇到了问题。我有一个姓名表和地址表。无论地址记录是否返回任何结果,我都想提取名称记录。如果我在下面的代码中有地址记录有效,但是一旦地址记录被删除,我将不再收到姓名记录。我正在尝试这个(NHibernate Left Outer Join),但它对我不起作用。有任何想法吗?
映射:
<?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>
正面:
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>();
}
}