使用外键的流畅 NHibernate 子类映射



我有这个对象:

class Person { Int32 id; String name; /*..*/ Adress adress; }
class Employee : Person { String e_g_Tax; /*..*/ Guid relationshipToManagmentId; }

映射的以下前提:
(a) "relationsToManagmentId" 应该是外键.
(b) "关系到管理"表是一个非映射表,(应用程序的旧部分)
(c) 测绘战略是TPT。(至少对于新对象:-)

映射,直到现在:

public class PersonMap : ClassMap<Person> {
  public PersonMap(){
    Id(x => x.id);
    Map (x => x.Nachname).Length(255).Not.Nullable();
    /*..*/
    References(x => x.Adresse).Class(typeof(Adresse)).Not.Nullable();
  }
}
public class EmployeeMap : SubclassMap<Employee>
    {
        public EmployeeMap()
        {
            Map(x => x.e_g_Tax, "enjoytax")
                .Not.Nullable();
            /*..*/
            Join("RelationshipToManagment", xJoin =>
            {
                //xJoin.Table("RelationshipToManagment");
                xJoin.Fetch.Join();
                xJoin.KeyColumn("ID");
                xJoin.Map(x => x.relationshipToManagmentId)
                    .Not.Nullable() ;
            }); // --> exception!!

我怎么写这个?

Join()只能在主键(属性 ID)上联接到另一个表,但您需要联接在外键列上。普通引用可以满足您的需求

class Employee : Person
{
    Management Management;
}
public EmployeeMap()
{
    References(x => x.Management).Column("relationshipToManagmentId");
}

更新:如果您需要RelationshipToManagment表中的只读信息,您可以使用公式属性

public EmployeeMap()
{
    Map(x => x.RelationshipToManagment).Formula("(SELECT m.Title FROM RelationshipToManagment m WHERE m.Id = relationshipToManagmentId)");
}

最新更新