NHibernate的映射:保存层次结构单一表而不鉴 [英] NHibernate Mapping: Save hierarchy to single table without discriminator

查看:157
本文介绍了NHibernate的映射:保存层次结构单一表而不鉴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常可扩展的,模块化的应用程序。我想映射的entites其他组件扩展。然而,我还需要在基类的工作。

I have a very extensible and modular application. I want to extend mapped Entites from other assemblies. Yet, I still need to operate on the base classes.

例如:

在大会父类的映射:

    public class PersonMap : ClassMap<Person> 
    {
        public PersonMap()
        {
            Table("Persons");

            Id(x => x.Id).GeneratedBy.Assigned();
        }
}

在组件B子类的映射:

public class EmployeeMap : SubclassMap<Employee>
{
    public EmployeeMap()
    {
        Table("Persons");
        Extends(typeof(Person));

        KeyColumn("Id");

        LazyLoad();

        HasMany<Assignments>(x => x.Assignments).KeyColumn("Id").Inverse().Cascade.AllDeleteOrphan().LazyLoad().NotFound.Ignore();
    }
}

现在,每当我在大会A的一些code创建人将其另存为受NHibernate的员工。这导致了类转换异常,由于代理,每当我救一个人并试图刷新其在大会A.大会A不得对组件B的依赖。

Now whenever I create a Person in some code of Assembly A it is saved as Employee by NHibernate. And that leads to a class cast exception due to proxying whenever I save a Person and try to refresh it in Assembly A. Assembly A must not have a dependency on Assembly B.

我需要的组件A的子类仅在其他组件中使用的所有方法在父类工作。

I need to operate on the parent class in all methods of assembly A. The child class is only used in other assemblies.

我该如何映射这样的事情?我怎么能告诉NHibernate的只是将其保存为父类?我用SaveOrUpdate坚持实体;我怎样才能正确地扩展实体,但将它们保存到同一个表而不鉴?不能NHibernate的区分按对象类型?有没有解决办法?

How can I map something like that? How can I tell NHibernate to just save it as the parent class? I use SaveOrUpdate to persist the entities; How can I correctly extend entities and yet save them to the same table without discriminator? Can't NHibernate differentiate by object type? Is there a workaround?

我不想手动指定代理,我不得不创造每一个实体的代理!我不能使用访问者模式由于依赖问题。

I do not want to specify manual proxies as I'd have to create a proxy for every entity! I cannot use the visitor pattern due to dependency problems.

我需要一种方法来扩展在不同的组件映射的实体,没有这样的问题!该数据库是旧的,我不能改变它。你将如何解决这个问题?

I need a way to extend a mapped entity in a different assembly without such problems! The database is legacy, I cannot change it. How would you work around the issue?

推荐答案

通过使用相同的表一HasOne映射,而不是使用一个子类,解决了这个问题。这不会产生理想的code,但问题reather免费的。

Solved it by using a HasOne Mapping on the same Table and not using a subclass. This does not produce ideal code but is reather free of problems.

这篇关于NHibernate的映射:保存层次结构单一表而不鉴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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