我有一个类DepartmentEntity
,包括一个名为Company(CompanyEntity)
波纹管的属性:
public class DepartmentEntity
{
public virtual int ID { get; set; }
public virtual string Name { get; set; }
public virtual DepartmentEntity Parent { get; set; }
public virtual CompanyEntity Company { get; set; }
}
public class CompanyEntity
{
public virtual int ID { get; set; }
public virtual string Name { get; set; }
}
DepartmentEntity.hbm.xml
如下:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="HS.DepartmentEntity, HS" table="DepartmentInfo" lazy="true">
<id name="ID">
<generator class="identity" />
</id>
<property name="Name" not-null="true" />
<many-to-one name="Parent" column="ParentID" class="HS.DepartmentEntity, HS" cascade="none" unique="true" not-found="ignore" lazy="no-proxy" />
<many-to-one name="Company" column="CompanyID" class="HS.CompanyEntity, HS" cascade="none" unique="true" not-found="ignore" lazy="no-proxy" />
</class>
</hibernate-mapping>
CompanyEntity.hbm.xml
如下:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="HS.CompanyEntity, HS" table="CompanyInfo" lazy="true">
<id name="ID">
<generator class="identity" />
</id>
<property name="Name" not-null="true" />
</class>
</hibernate-mapping>
我尝试了下面的代码:
IList<DepartmentEntity> list;
using(ISession session = GetSession())
{
string hql = "FROM DepartmentEntity as dpe join fetch dpe.Company";
list = session.CreateQuery(hql).List<DepartmentEntity>();
}
会话关闭后,Company
无法访问属性,但如果列表方法只找到一条记录,则Company
可以访问属性,我不知道为什么。