2013-10-02 48 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结构创建关系。然后我们可以通过对象模型的抽象来查询数据库 –