2013-10-02 49 views
1

我的遺留表「ALLDATA」有這些列:Id, Title, LookupColumn1Id 我的實體:地圖企業自我連接表憑身份證

public class BaseEntity 
{ 
    public virtual int Id { get; set; } 
    public virtual string Name { get; set; } 
} 
public class Employee: BaseEntity 
{ 
    public virtual int DepartmentId { get; set; } 
    public virtual string DepartmentName { get; set; } 
} 
public class Department: BaseEntity 
{ 
    public virtual int HeadManagerId { get; set; } 
} 

我要生成選擇這樣的:

SELECT EmployeeTable.Title, DepartmentTable.Id, DepartmentTable.Title 
FROM AllData EmployeeTable left outer join AllData DepartmentTable on EmployeeTable.LookupColumn1Id=DepartmentTable.Id  
WHERE EmployeeTable.tp_ListId = @p0 and (DepartmentTable.Title = @p1)  

回答

0

設我告訴你,其中一個選項。對於這個草稿,我期望,那些具有LookupColumn1Id NULL的記錄將扮演Department的角色,其餘將扮演Employee的角色。

實體看起來是這樣的:

public class BaseEntity 
{ 
    public virtual int Id { get; set; } 
    public virtual string Name { get; set; } 
} 
public class Employee : BaseEntity 
{ 
    public virtual Department Department { get; set; } // to lookup record 
} 
public class Department : BaseEntity 
{ 
    public virtual IList<Employee> Employees { get; set; } // the way back 
} 

的映射可能是這樣的:

<class name="Department" table="[dbo].[AllData]" lazy="true" batch-size="25" 
    where="LookupColumn1Id IS NULL" > 
    <id name="Id" column="Id" generator="native" /> 

    <property not-null="true" name="Name" column="Title" /> 

    <bag name="Employees" > 
    <key column="LookupColumn1Id" /> 
    <one-to-many class="Employee"/> 
    </bag> 

</class> 

<class name="Employee1" table="[dbo].[AllData]" lazy="true" batch-size="25" 
    where="LookupColumn1Id IS NOT NULL" > 
    <id name="Id" column="Id" generator="native" /> 

    <property not-null="true" name="Name" column="Title" /> 

    <many-to-one name="Department" class="Department" column="LookupColumn1Id " /> 
</class> 

這種映射,讀訪問(要求SELECT)工作。現在,我們可以創建一個查詢:

[TestMethod] 
public void TestAllData() 
{ 
    var session = NHSession.GetCurrent(); 

    // the Employee Criteria 
    var criteria = session.CreateCriteria<Employee>(); 
    // joined with the Department 
    var deptCrit = criteria.CreateCriteria("Department", JoinType.LeftOuterJoin); 

    // here we can filter Department 
    deptCrit.Add(Restrictions.Eq("Name", "Dep Name")); 
    // here we can filter Employee 
    criteria.Add(Restrictions.Eq("Name", "Emp Name")); 


    // the SELECT 
    var results = criteria 
     .List<Employee>(); 

    Assert.IsTrue(results.IsNotEmpty()); 

    var employee = results.First(); 

    // check if all data are injected into our properties 
    Assert.IsTrue(employee.Name.IsNotEmpty()); 
    Assert.IsTrue(employee.Department.Name.IsNotEmpty()); 
} 

這種情況在一般的工作,但我們所做的,只是在C#中(無論是從BaseEntity派生)繼承,而不是在映射。

原因是,缺失的列將扮演Discriminator角色。這就是爲什麼我們使用WHERE屬性的映射(請參閱xml中的類元素),通過查找列存在區分DepartmentEmployee

+0

謝謝,但它不是答案。我不會在代碼中顯式實現該層上的實體之間的關係,我只想在映射中指定該特定關係。 – dbardakov

+0

關鍵是,你正在嘗試使用NHibernate,這是ORM。換句話說,您將表訪問移動到實體/對象訪問。這意味着,我們必須將表映射到實體,根據它們的DB和Object結構創建關係。然後我們可以通過對象模型的抽象來查詢數據庫 –