私はクラスを持っており、次のような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
にアクセスできませんが、リスト メソッドが 1 つのレコードしか見つからない場合、プロパティCompany
にアクセスできます。理由はわかりません。