Fluent NHibernate在映射中连接表而不使用主键

时间:2011-04-29 18:04:34

标签: nhibernate fluent-nhibernate nhibernate-mapping

我正在尝试从2个不与主键相关的表中创建一个实体

表格

CREATE TABLE [employees](
    [ssn] [nvarchar](9) NULL,
    [active] [bit] NULL,    
    [employee_id] [int] IDENTITY(1,1) NOT NULL 
)


CREATE TABLE [sam_employees](
    [ssn] [nvarchar](9) NULL,
    [first_name] [nvarchar](50) NULL,
    [last_name] [nvarchar](50) NULL,
    [skill] [nvarchar](50) NULL,
    ....
)

要生成的SQL:

SELECT this_.employee_id  as employee1_0_0_,
       this_.ssn          as ssn0_0_,
       this_.active       as active0_0_,
       this_1_.first_name as first2_1_0_,
       this_1_.last_name  as last3_1_0_,
       this_1_.skill      as skill1_0_
FROM   employees this_
       inner join sam_employees this_1_
         on this_.ssn = this_1_.ssn
WHERE  this_.active = 1

我当前的映射:

public class EmployeeMap : ClassMap<Employee>
{
    public EmployeeMap()
    {
        Table("employees");
        Id(x => x.Id, "employee_id");
        Map(x => x.SSN, "ssn");
        Map(x => x.IsActive, "active");

        Join("sam_employees", mm =>
                                  {                                          
                                      mm.KeyColumn("ssn");
                                      mm.Map(xx => xx.FirstName, "first_name");
                                      mm.Map(xx => xx.LastName, "last_name");
                                      mm.Map(xx => xx.Skill, "skill");
                                  });            
    }
}

但是这个映射我在this_.employee_id = this_1_.ssn上有这样的连接条件

我知道之前曾问过这个问题,但我找不到一个好的答案,只是解决方法在数据库端使用View而不是表。

1 个答案:

答案 0 :(得分:3)

您可以尝试将sam_employees映射为实体,并使用带有连接类型提取的引用映射。