基于另一列的Fluent-NHibernate多级继承 [英] Fluent-NHibernate Multi-Level Inheritance based on another column

查看:120
本文介绍了基于另一列的Fluent-NHibernate多级继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题找到我有问题的良好文档。我正在重建一个企业解决方案的一部分,并通过NHibernate为我们的ORM交换。

MasterAccount:Account:InvBilling



<$ p $ MasterAccount **
id_MasterAccount
$ b **帐户**
id_Account
id_MasterAccount
id_InvBilling

** InvBilling **
id_InvBilling
id_MasterAccount

MasterAccount中的行存在Id_MasterAccount列相同且IsMaster列为1的Account中的行。但是,Account表中还有其他id_MasterAccounts等于与其父帐户关联的记录。最后,Account表中的每条记录都在InvBilling表中有一条记录。



我在设置映射时遇到了很多问题。任何帮助如何设置?

我需要告诉MasterAccount它应该将自己关联到哪个帐户记录以及它应该使用的列。



编辑:这是我到目前为止:

  public class MasterAccountEntity: AccountEntity {
protected virtual int MAId {get; set;}
}

public class AccountEntity:InvBillingEntity {
protected virtual int AId {get; set;}
public virtual MasterAccountEntity MA {get; set;}
public virtual InvBillingEntity IB {get; set;}
public virtual bool IsMaster {get; set;}
}

public class InvBilling {
protected virtual int IBId {get; set;}
public MasterAccountEntity MA {get; set;}
}



然后对于我的映射(在如何显示继承方面有些遗憾):

Table(MasterAc计数);
Map(x => x.MAId).Column(id_MasterAccount);


public class FluentHibernateMap:SubclassMap< AccountEntity> {
Table(Account);
Map(x => x.AId).Column(id_Account);
Map(x => x.IsMaster).Column(isMaster);
引用(x => x.MA).Column(id_MasterAccount)。Not.Nullable()。Cascade.None();
References(x => x.IB).Column(id_InvBilling)。Not.Nullable()。Cascade.None();
}

public class FluentHibernateMap:ClassMap< InvBillingEntity> {
Table(InvBilling);
Id(x => x.IBId).Column(id_InvBilling);
引用(x => x.MA).Column(id_MasterAccount)。Not.Nullable()。Cascade.None();





$ b因此,MasterAccount需要继承IsMaster为true的账户中的记录,然后继承InvBilling中与Account中的InvBillingId相关的记录。

解决方案

账户表是一个版本管理表,它有效地处理了多个一边到主表。我会反映,而不是试图隐藏在映射中。那么就不可能对历史记录做任何事情,复杂性也就不必要了。

  public class实体
{
protected virtual int Id {get;私人设置} //应该只读一次由NHibernate设置

$ b $ public class AccountMasterEntry:Entity
{
public virtual Account ActiveAccount {get;私人设置;}
}

公共类帐户:实体
{
public virtual AccountMasterEntry MA {get; set;}
public virtual InvBillingEntity IB { get; set;}
public virtual bool Active {get; set;}
}

public class InvBilling:Entity
{
}

public class AccountMasterEntryMap:ClassMap< AccountMasterEntry>
{
public AccountMasterEntryMap()
{
Id(x => x.Id).GeneratedBy ...;

//可选
引用(x => x.ActiveAccount).Formula((SELECT acc.id_Account FROM Account acc WHERE id_MasterAccount = acc.id_MasterAccount AND acc.IsMaster = 1) );
}
}

public class AccountMap:ClassMap< Account>
{
public AccountMap()
{
Id(x => x.Id).GeneratedBy ...;

参考文献(x => x.MA,id_MasterAccount);
参考文献(x => x.IB,id_InvBilling);
Map(x => x.Active,ismaster);
}
public virtual AccountMasterEntry MA {get;组; }
public virtual InvBilling IB {get; set;}
public virtual bool IsMaster {get; set;}
}

public class InvBilling:Entity
{
}

//查询
int masterId = ...;
var accountrecord = session.Query< Account>()
.Where(a => a.MA.Id == masterId&& a.Active)// NHibernate应该足够聪明看到没有连接需要
.Single();

注意:我不确定是否invbillingId是唯一的,或者是组合ID


I am having issues finding good documentation for the problem I am having. I am reconstructing a portion of an enterprise solution and swapping over the NHibernate for our ORM. I have 3 tables which I want to all inherit from each other.

MasterAccount : Account : InvBilling

**MasterAccount**
id_MasterAccount

**Account**
id_Account
id_MasterAccount
id_InvBilling

**InvBilling**
id_InvBilling
id_MasterAccount

For each row in MasterAccount there exists a row in Account where the Id_MasterAccount columns are equal and the IsMaster column is 1. However, there are also other records in the Account table which also have their id_MasterAccounts equal to associate with their parent account. Finally, every record in the Account table has a record in the InvBilling table.

I am having lots of issues setting up their mappings. Any help with how to set this up?

I need to somehow tell MasterAccount which Account record it should associate itself to as well as the column it should use.

Edit: Here is what I have so far:

public class MasterAccountEntity : AccountEntity{
    protected virtual int MAId {get;set;}
}

public class AccountEntity : InvBillingEntity{
    protected virtual int AId {get;set;}
    public virtual MasterAccountEntity MA {get;set;}
    public virtual InvBillingEntity IB {get;set;}
    public virtual bool IsMaster {get;set;}
}

public class InvBilling{
    protected virtual int IBId {get;set;}
    public MasterAccountEntity MA {get;set;}
}

Then for my mappings(somewhat lost on how to show the inheritance):

public class FluentHibernateMap : SubclassMap<MasterAccountEntity>{
    Table("MasterAccount");
    Map(x => x.MAId).Column("id_MasterAccount");
}

public class FluentHibernateMap : SubclassMap<AccountEntity>{
    Table("Account");
    Map(x => x.AId).Column("id_Account");
    Map(x => x.IsMaster).Column("isMaster");
    References(x => x.MA).Column("id_MasterAccount").Not.Nullable().Cascade.None();
    References(x => x.IB).Column("id_InvBilling").Not.Nullable().Cascade.None();
}

public class FluentHibernateMap : ClassMap<InvBillingEntity>{
    Table("InvBilling");
    Id(x => x.IBId).Column("id_InvBilling");
    References(x => x.MA).Column("id_MasterAccount").Not.Nullable().Cascade.None();
}

So MasterAccount needs to inherit the record from Account where IsMaster is true, and then inherit the record in InvBilling that cooresponds to the InvBillingId in Account.

解决方案

Account table is a Versioning table and it is effectivly the many side to the master table. i would reflect that and not try to hide this in the mapping. Then it would be impossible to do anything with the historic records and complexity rises without need.

public class Entity
{
    protected virtual int Id { get; private set; }  // should be readonly once set by NHibernate
}

public class AccountMasterEntry : Entity
{
    public virtual Account ActiveAccount { get; private set;}
}

public class Account : Entity
{
    public virtual AccountMasterEntry MA {get;set;}
    public virtual InvBillingEntity IB {get;set;}
    public virtual bool Active {get;set;}
}

public class InvBilling : Entity
{
}

public class AccountMasterEntryMap : ClassMap<AccountMasterEntry>
{
    public AccountMasterEntryMap()
    {
        Id(x => x.Id).GeneratedBy...;

        // optional
        References(x => x.ActiveAccount).Formula("(SELECT acc.id_Account FROM Account acc WHERE id_MasterAccount = acc.id_MasterAccount AND acc.IsMaster = 1)");
    }
}

public class AccountMap : ClassMap<Account>
{
    public AccountMap()
    {
        Id(x => x.Id).GeneratedBy...;

        References(x => x.MA, "id_MasterAccount");
        References(x => x.IB, "id_InvBilling");
        Map(x => x.Active, "ismaster");
    }
    public virtual AccountMasterEntry MA { get; set; }
    public virtual InvBilling IB {get;set;}
    public virtual bool IsMaster {get;set;}
}

public class InvBilling : Entity
{
}

// query
int masterId = ...;
var accountrecord = session.Query<Account>()
    .Where(a => a.MA.Id == masterId && a.Active)  // NHibernate should be smart enough to see that there is no join needed
    .Single();

Note: i'm not sure if invbillingId alone is unique or if it is a compositeid

这篇关于基于另一列的Fluent-NHibernate多级继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆